1、首先在weblogic中建立数据源jdbc/test
2、编写访问jdbc/test的java代码
package com.hunau.liuyong;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Conn {
private Connection conn;
private DataSource ds;
public Conn() {
try {
Context initCtx = new InitialContext();
if (initCtx == null) {
throw new Exception("上下文加载失败");
}
//获取在weblogic中配置的“jdbc/test”数据源
ds = (DataSource) initCtx.lookup("jdbc/test");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Connection getConn() {
try {
conn = ds.getConnection();
} catch (SQLException ex) {
ex.printStackTrace();
}
return conn;
}
public void closeConn(Connection conn) {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
3、action,servlet,jsp中通过Conn去获得连接访问数据库的连接,其余步骤和普通jdbc操作一样