1.在EJB服务器上建立数据源,我用的服务器是weblogic7,配置如下:
Connection Pools
Name:MyJDBC
URL:jdbc
dbc:Northwind //Northwind是ODBC数据源名,我是连接到SQL server 7
的Northwind数据库,你只需要改为连接到ORACLE的数据
库就行了
Driver:sun.jdbc.odbc.JdbcOdbcDriver
Properties
user=sa,password=) //连接数据库的账号、密码
Tx Data Sources
Name:MyJDBC
JNDI Name:MyJDBC
Pool Name:MyJDBC
2.jsp代码如下
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="java.util.*"%>
<html>
<body>
<%
Context initialContext=new InitialContext();
DataSource sds=(DataSource)initialContext.lookup("MyJDBC");
Connection connection=sds.getConnection("system","masterkey");
//访问EJB服务器的账号、密码
Statement stmt=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from employees";
ResultSet rs=stmt.executeQuery(sql);
<table border="1">
<%while(rs.next()) {%>
<tr><td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td></tr>
<%}%>
</table>
<%out.print("數據庫操作成功,恭喜你!");%>
<%rs.close();
stmt.close();
connection.close();
%>
</body>
</html>