首页上一页 1 下一页尾页 1 条记录 1/1页
有关连接池问题
发表在JavaWeb图书答疑
2009-12-03
是否精华
是
否
版块置顶:
是
否
老师:
您好!
请问在JSP中有几种方式设定连接池与访问连接池方法(其中数据库用户与非空密码),请详述!谢谢!!!
package com.eip.dao;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
import java.util.Date;
public class DBResult {
public static Connection conn = null;
private static Connection con;
ResultSet rs; /*
//private static Context ctx;
//private static Context env;
//Date date=new Date(); */
static{
try {
Context ctx = new InitialContext();
ctx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("jdbc/eip"); // 获取连接池对象
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (NamingException e) {
e.printStackTrace();
}
}
public static synchronized Connection getConnection() throws Exception{
try{
Context ctx=new InitialContext();
Context env=(Context)ctx.lookup("java:comp/env");
DataSource ds=(DataSource)env.lookup("jdbc/eip");
return con=ds.getConnection();
}
catch(SQLException e){
throw e;
}
catch(NamingException e){
throw e;
}
}
/**
* 用于获得执行SQL语句的ResultSet对象
*/
public ResultSet getResult(String sql) {
try {
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
//ResultSet rs = stmt.executeQuery(sql);
return rs;
} catch (Exception e) {
}
return null;
}
/**
* 用于执行SQL语句没有返回值
*/
public void doExecute(String sql) {
try {
Statement stmt = conn.createStatement();
stmt.executeQuery(sql);
} catch (Exception e) {
}
}
/**
* 用于获得执行SQL语句的PreparedStatement(预处理)对象
*/
public PreparedStatement getPreparedStatement(String sql) {
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
return pstmt;
} catch (Exception e) {
}
return null;
}
public int executeUpdate(String sql){
int result=0;
try {
Statement stmt=conn.createStatement();
result=stmt.executeUpdate(sql);
}catch(SQLException eer){
System.out.print(eer.getMessage());
}
return result;
}
public ResultSet executeQuery(String sql){
try{
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=stmt.executeQuery(sql);
return rs;
}catch(SQLException er){
System.err.print(er.getMessage());
}
return null;
}
public void close(){
try{
if(con!=null){
con.close();
}
}catch(Exception err){
System.out.print(err);
}
try{
if(rs!=null){
rs.close();
}
}catch(Exception err){
System.out.print(err);
}
}
}
您好!
请问在JSP中有几种方式设定连接池与访问连接池方法(其中数据库用户与非空密码),请详述!谢谢!!!
package com.eip.dao;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
import java.util.Date;
public class DBResult {
public static Connection conn = null;
private static Connection con;
ResultSet rs; /*
//private static Context ctx;
//private static Context env;
//Date date=new Date(); */
static{
try {
Context ctx = new InitialContext();
ctx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("jdbc/eip"); // 获取连接池对象
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (NamingException e) {
e.printStackTrace();
}
}
public static synchronized Connection getConnection() throws Exception{
try{
Context ctx=new InitialContext();
Context env=(Context)ctx.lookup("java:comp/env");
DataSource ds=(DataSource)env.lookup("jdbc/eip");
return con=ds.getConnection();
}
catch(SQLException e){
throw e;
}
catch(NamingException e){
throw e;
}
}
/**
* 用于获得执行SQL语句的ResultSet对象
*/
public ResultSet getResult(String sql) {
try {
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
//ResultSet rs = stmt.executeQuery(sql);
return rs;
} catch (Exception e) {
}
return null;
}
/**
* 用于执行SQL语句没有返回值
*/
public void doExecute(String sql) {
try {
Statement stmt = conn.createStatement();
stmt.executeQuery(sql);
} catch (Exception e) {
}
}
/**
* 用于获得执行SQL语句的PreparedStatement(预处理)对象
*/
public PreparedStatement getPreparedStatement(String sql) {
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
return pstmt;
} catch (Exception e) {
}
return null;
}
public int executeUpdate(String sql){
int result=0;
try {
Statement stmt=conn.createStatement();
result=stmt.executeUpdate(sql);
}catch(SQLException eer){
System.out.print(eer.getMessage());
}
return result;
}
public ResultSet executeQuery(String sql){
try{
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=stmt.executeQuery(sql);
return rs;
}catch(SQLException er){
System.err.print(er.getMessage());
}
return null;
}
public void close(){
try{
if(con!=null){
con.close();
}
}catch(Exception err){
System.out.print(err);
}
try{
if(rs!=null){
rs.close();
}
}catch(Exception err){
System.out.print(err);
}
}
}