当前位置: 代码迷 >> J2SE >> SQL Server2000数据库连接池的有关问题
  详细解决方案

SQL Server2000数据库连接池的有关问题

热度:67   发布时间:2016-04-24 15:54:20.0
SQL Server2000数据库连接池的问题
请问哪位高手能帮我解决SQL   Server2000数据库连接池的问题啊,如果可以的话麻烦贴出代码,非常感谢!!

------解决方案--------------------
<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.*,javax.naming.*,javax.sql.* " errorPage= " " %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN " "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> 连接池连接MS SQL Server 2000 </title>
</head>
<body>
<%
out.print( "使用连接池连接MS SQL Server 2000 <br> ");
out.println( " <br> ");
Context ctx=null;
DataSource ds=null;
Statement stmt=null;
ResultSet rs=null;
Connection con=null;
ResultSetMetaData md=null;
try{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup( "java:comp/env/jdbc/Project ");
con=ds.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery( "select * from Test ");
md=rs.getMetaData();
out.print(md.getColumnLabel(1)+ " ");
out.print(md.getColumnLabel(2)+ " <br> ");
while(rs.next()){
out.print(rs.getInt(1)+ " ");
out.print(rs.getString(2)+ " <br> ");
}
}catch(Exception e){
out.print(e);
}finally{
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
if(con!=null)con.close();
}
%>
</body>
</html>
------解决方案--------------------
你要配置数据库连接池吗 配置的server.xml 加在 </Host> 的上面
<Context path= "/db2test " docBase= "db2test "
reloadable= "true " crossContext= "true ">


<ResourceParams name= "jdbc/db2test ">


<parameter>
<name> maxWait </name>
<value> 5000 </value>
</parameter>

<parameter>
<name> maxActive </name>
<value> 6 </value>
</parameter>



<parameter>
<name> password </name>
<value> cgm </value>
</parameter>

<parameter>
<name> url </name>
<value> jdbc:oracle:thin:@127.0.0.1:1521:demo </value>
</parameter>



<parameter>
<name> driverClassName </name>
<value> oracle.jdbc.driver.OracleDriver </value>
</parameter>
<parameter>
<name> maxIdle </name>
<value> 2 </value>
</parameter>
<parameter>
<name> username </name>
<value> system </value>
</parameter>
</ResourceParams>


</Context>


获得Connection的代码
public class DB2JNDI {

public Connection getConnectin(){
Connection cn = null;
try{
Context ctx = new InitialContext();

System.out.println( "获得上下问对象 ");

DataSource ds = (DataSource) ctx.lookup( "java:comp/env/jdbc/test ");
System.out.println( "获得数据源 ");
  相关解决方案