JBuilder9+Weblogic7实战篇
JSP通过javaBean调用JDBC篇
作者:黄 凯
E_mail:hk_sz@163.com
运用篇 JSP+javaBean+JDBC+Oracle
一、启动JBuilder
启动JBuilder: 开始/程序/Borland JBuilder 9 Enterprise Trial/Borland JBuilder 9 Enterprise Trial;
在操作下面的步骤前请先检查一下你的配置和参数是否与《JBuilder9+Weblogic7实战篇之工具篇(ORACLE9i)》、《JBuilder9+Weblogic7实战篇之工具篇(JDBC 1)》一致。
以下没有详细说明部分如有不明白的请参看《JBuilder9+Weblogic7实战篇之JSP+SERVLET篇》。
二、创建一个工程
创建一个名为search的工程。
三、设置 Web Application
创建一个名为search的Web Application。
四、创建 JSPs
4.1创建search.jsp文件;
4.2在代码区所有代码用下面代码替换:
<%@ page contentType="text/html; charset=GBK"
import="java.util.*" import="java.sql.ResultSet" import="search.dbBean"%>
<html>
<head>
<title>
search
</title>
</head>
<body bgcolor="#ffffff">
<center>
<h1>
JSP-->javaBean-->JDBC-->Oracle
</h1>
</center>
<%
dbBean conn = new dbBean();
ResultSet rs = null;
rs = conn.executeQuery("SELECT * FROM count");
ArrayList validates = new ArrayList();
while (rs.next())
{
String num1 = rs.getString("num1").trim();
String num2 = rs.getString("num2").trim();
String result = rs.getString("result").trim();
%>
<table width="100%" cellspacing="0" cellpadding="5" align="center">
<tr>
<td><B>num1</b></td>
<td><B>num2</b></td>
<td><B>result</b></td>
</tr>
<tr>
<td><%=num1%></td>
<td><%=num2%></td>
<td><%=result%></td>
</tr>
</table>
<%
}
conn.closeConnection();
%>
</body>
</html>
五、创建 javaBean
5.1在菜单栏选择File/New...,弹出Object Gallery窗口;
5.2单击General选项卡;
5.3选择JavaBean然后单击OK,出现JavaBean的向导页面;
5.4在Class name后输入dbBean;单击OK完成。
5.5在代码区将所有代码用以下代码替换:
package search;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.naming.*;
import javax.sql.*;
public class dbBean
implements Serializable
{
DataSource ds = null;
Context ctx = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public void getConnection()
{
try
{
//获得Weblogic Server JBDI初始上下文信息
ctx = getInitialContext();
System.out.println(ctx.toString());
//查找JNDI名字为jdbc/testTxDataSource的数据源,建立数据源对象
ds = (javax.sql.DataSource) ctx.lookup("jdbc/testTxDataSource");
System.out.println(ds.toString());
//通过数据源对象建立数据连接
conn = ds.getConnection();
System.out.println(conn.toString());
//建立语句对象
stmt = conn.createStatement();
}
catch (java.sql.SQLException es)
{
System.out.println("mySQLException" + es.getMessage());
}
catch (Exception ex)
{
System.out.println("conn is error" + ex.getMessage());
}
}
public void closeConnection()
{
try
{
if (rs != null)
//关闭结果集
rs.close();
if (stmt != null)
//关闭语句对象
stmt.close();
if (conn != null)
//关闭连接
conn.close();
}
catch (java.sql.SQLException e)
{
System.out.println("mycloseConnectioinSQL" + e.getMessage());
rs = null;
stmt = null;
conn = null;
}
}
public ResultSet executeQuery(String sql)
{
rs = null;
try
{
if (conn == null)
getConnection();
if (conn != null)
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch (Exception e)
{
System.out.println("executeQuery error here: " + e.getMessage());
}
return rs;
}
public boolean executeUpdate(String sql)
{
boolean bupdate = false;
try
{
if (conn == null)
getConnection();
if (conn != null)
{
stmt = conn.createStatement();
int rowCount = stmt.executeUpdate(sql);
if (rowCount != 0)
bupdate = true;
}
}
catch (SQLException ex)
{
System.err.println("Conn.executeUpdate" + ex.getMessage());
}
return bupdate;
}
//getInitialContext()方法用来获得Weblogic Server的初始上下文环境以进行JNDI查询。
private static Context getInitialContext() throws Exception
{
//url、user、password分别对应Weblogic Server的地址、端口号、系统用户及口令
String url = "t3://localhost:7001";
String user = "training";
String password = "training";
Properties properties = null;
try
{
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password);
return new InitialContext(properties);
}
catch (Exception e)
{
throw e;
}
}
/*Bean测试用
public static void main(String args[])
{
ResultSet rs = null;
try
{
dbBean connData = new dbBean();
//建立结果集对象
rs = connData.executeQuery("SELECT * FROM count");
//遍历结果集对象,访问每一条记录,输出数据
while (rs.next())
{
String num1 = rs.getString("num1").trim();
String num2 = rs.getString("num2").trim();
String result = rs.getString("result").trim();
System.out.println("validate: \'" + num1 + "+" + num2 + "=" +
result + "\" is ");
}
}
catch (Exception e)
{}
}*/
}
完成后运行查看结果。
我的文章首发牛耳论坛(www.newer.com.cn/bbs)和程序员论坛(www.csdn.net),欢迎转载,不过,请保留作者以及修订者的名字,谢谢。