主题:数据连接池连接数据出错
在tomcat的server.xml中插入如下内容:
<Context path="/ch07" docBase="ch07" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/cn" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/cn">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=student</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value> </value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>
</Context>
用于获得连接的类:
package bole;
import java.sql.*;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class DatabaseConn
{
public static Connection getConnection() throws Exception
{
try
{
Context initCtx = new javax.naming.InitialContext();
Context envCtx=(Context)initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/bn");
return ds.getConnection();
}
finally
{
}
}
}
使用数据源的servlet:
package bole;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.io.*;
public class DataSourceTestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
try
{
Connection con=DatabaseConn.getConnection();
Statement stmt = con.createStatement();
//stmt.execute("select * from course");
ResultSet rst = stmt.executeQuery("select * from course");
while(rst.next())
{
out.println(rst.getString("id"));
out.println(rst.getString("name"));
}
stmt.close();
con.close();
}
}
}
可是运行时出现错误,东西比较多,麻烦各位了
<Context path="/ch07" docBase="ch07" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/cn" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/cn">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=student</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value> </value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>
</Context>
用于获得连接的类:
package bole;
import java.sql.*;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class DatabaseConn
{
public static Connection getConnection() throws Exception
{
try
{
Context initCtx = new javax.naming.InitialContext();
Context envCtx=(Context)initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/bn");
return ds.getConnection();
}
finally
{
}
}
}
使用数据源的servlet:
package bole;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.io.*;
public class DataSourceTestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
try
{
Connection con=DatabaseConn.getConnection();
Statement stmt = con.createStatement();
//stmt.execute("select * from course");
ResultSet rst = stmt.executeQuery("select * from course");
while(rst.next())
{
out.println(rst.getString("id"));
out.println(rst.getString("name"));
}
stmt.close();
con.close();
}
}
}
可是运行时出现错误,东西比较多,麻烦各位了