在jsp中配置数据库连接池的步骤.
1.要在tomcat下的conf包中的server.xml中加入数据库连接池配置信息
- <Context path="/jdbc" docBase="D:/workspace/jdbc/WebRoot" >
- <Resource auth="Container" name="jdbc/hhh" type="javax.sql.DataSource" factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
- driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/hanyue"
- username="root"
- password=""
- maxActive="10000"
- maxIdle="10000"
- maxWait="10000"
- removeAbandoned="true"
- removeAbandonedTimeOut="10"
- logAbandoned="true"/>
- </Context>
1.确认common/lib/naming-factory-dbcp.jar存在.添加factory的时候一定要写org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory,
而不能写org.apache.commons.dbcp.BasicDataSourceFactory,否则会报找不到驱动程序的url的错误.
2.注意:
- <%@ page import="java.sql.*" %>
- <%@ page import="javax.sql.*" %>
- <%@ page import="javax.naming.*" %>
- <%@ page contentType="text/html;charset=GB2312" %>
- <html>
- <head>
- <title>my jdbc action</title>
- </head>
- <body>
- <h2>登陆结果</h2>
- <%
- Connection con = null;
- Statement stmt = null;
- Statement stmt1 = null;
- ResultSet rs = null;
- String url = "jdbc:mysql://localhost:3306/hanyue";
- String uname = "root";
- String upasswd = "";
- DataSource ds = null;
- String id = "13";
- String name = request.getParameter("name");
- String passwd = request.getParameter("passwd");
- String new_last_name = "";
- String new_first_name = "";
- %>
- 从login.html得到值
- name = <%= name %>
- passwd = <%= passwd %><br><br>
- <%
- try
- {
- //找到驱动程序并注册
- Context initContext = new InitialContext();
- Context envContext = (Context)initContext.lookup("java:/comp/env");
- DataSource dst = (DataSource)envContext.lookup("jdbc/hhh");
- //ds = (DataSource) initContext.lookup("java:comp/env/jdbc/hhh");
- con = dst.getConnection();
- if(envContext==null){
- System.out.println("envContext is null");
- }else{
- System.out.println("envContext is not null");
- }
- if(dst==null){
- System.out.println("dst is null");
- }else{
- System.out.println("dst is not null");
- }
- if(con==null){
- System.out.println("con is null");
- }else{
- System.out.println("con is not null");
- }
- //stmt = con.createStatement();
- // Class.forName("org.gjt.mm.mysql.Driver");
- // con = DriverManager.getConnection(url,uname,upasswd);
- // stmt = con.createStatement();
- //String upd = "insert into person values('"+id+"','"+name+"','"+passwd+"')";
- //stmt.executeUpdate(upd);
- stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
- String query = "SELECT * FROM person";
- rs = stmt1.executeQuery(query);
- rs.last( );
- new_last_name = rs.getString("name");
- new_first_name = rs.getString("password");
- }
- catch(SQLException e){
- e.printStackTrace();
- }
- finally
- {
- try {
- if(con != null)
- {
- con.close();
- }
- }
- catch(SQLException sqle)
- {
- out.println("sqle="+sqle);
- }
- }
- %>
- 从数据库中取得的数据:
- <% out.println("name = "+new_last_name);
- out.println("passwd = "+new_first_name);%>
- </body>
- </html>
测试界面:
<html>
<head>
<title>my jdbc</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
<body>
<h2>登陆信息</h2>
<form name="form" action="action.jsp" method="post" >
<p><input name="name" type="text"></p>
<p><input name="passwd" type="password"></p>
<p>
<input type="submit" value="确定">
<input type="reset" value="清空">
</p>
</form>
</body>
</html>
数据库用mysql...测试结果...打印出了刚存入的数据.数据库连接池测试成功!!!!
问题:为什么用org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory的不行.
为什么该工程下的web.xml的东西注册不注册都行???
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/hanyue</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>