<%@ page language="java" import="com.mchange.v2.c3p0.*" import="java.sql.*" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <!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=gbk"> <title>加载包的形式使用c3p0连接池</title> </head> <body> <% Connection con=null; Statement stmt=null; ResultSet rs=null; try{ //创建数据源对象 ComboPooledDataSource cpds=new ComboPooledDataSource(); cpds.setDriverClass("com.mysql.jdbc.Driver"); cpds.setJdbcUrl("jdbc:mysql://localhost:3306/bookdb"); cpds.setUser("root"); cpds.setPassword("888"); //通过数据源得到连接对象 con=cpds.getConnection(); //得到命令对象 stmt=con.createStatement(); //执行查询命令 rs=stmt.executeQuery("select * from books"); //输出查询结果 out.print("<table border=1 align='center'>"); out.print("<tr><td>编号</td><td>作者</td><td>书名</td><td>价格</td><td>出版时间</td><td>图书描述</td><td>数量</td></tr>"); while(rs.next()){ String col1=rs.getString(1); String col2=rs.getString(2); String col3=rs.getString(3); float col4=rs.getFloat(4); int col5=rs.getInt(5); String col6=rs.getString(6); int col7=rs.getInt(7); //打印获取到的数据 out.print("<tr><td>"+col1+"</td><td>"+col2+"</td><td>"+col3+"</td><td>"+col4+"</td><td>"+col5+"</td><td>"+col6+"</td><td>"+col7+"</td></tr>"); } out.print("</table>"); }catch(Exception e){ //捕获到的错误信息打印出来 out.print(e.getMessage()); } %> </body> </html>
c3p0jstlReSourceTest.jsp
<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <!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=gbk"> <title>使用c3p0连接池jstl 测试sql</title> </head> <body> <sql:query var="rs" dataSource="c3p0/bookdb">select * from books</sql:query> <table border=1 align='center'> <tr><td>编号</td><td>作者</td><td>书名</td><td>价格</td><td>出版时间</td><td>图书描述</td><td>数量</td></tr> <c:forEach var="row" items="${rs.rows}"> <tr> <td>${row.id}</td> <td>${row.name}</td> <td>${row.title}</td> <td>${row.price}</td> <td>${row.yr}</td> <td>${row.description}</td> <td>${row.saleAmount}</td> </tr> </c:forEach> </table> </body> </html>
测试连接MySql数据库
<%@ page language="java" import="java.util.*" import="java.io.*" import="java.sql.*" pageEncoding="gbk"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>测试连接MySql数据库</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% Connection con=null; Statement stmt=null; ResultSet rs=null; try{ //加载MySql驱动程序 Class.forName("com.mysql.jdbc.Driver"); //连接数据库字符串 String dbUrl="jdbc:mysql://localhost:3306/bookdb"; String dbUser="root"; String dbPwd="888"; //获得数据库连接 con=DriverManager.getConnection(dbUrl,dbUser,dbPwd); //得到命令对象 stmt=con.createStatement(); //查询得到结果集 rs=stmt.executeQuery("select * from books"); //输出查询结果 out.print("<table border=1 align='center'>"); out.print("<tr><td>编号</td><td>作者</td><td>书名</td><td>价格</td><td>出版时间</td><td>图书描述</td><td>数量</td></tr>"); while(rs.next()){ String col1=rs.getString(1); String col2=rs.getString(2); String col3=rs.getString(3); float col4=rs.getFloat(4); int col5=rs.getInt(5); String col6=rs.getString(6); int col7=rs.getInt(7); //打印获取到的数据 out.print("<tr><td>"+col1+"</td><td>"+col2+"</td><td>"+col3+"</td><td>"+col4+"</td><td>"+col5+"</td><td>"+col6+"</td><td>"+col7+"</td></tr>"); } out.print("</table>"); }catch(Exception e){ //捕获到的错误信息打印出来 out.print(e.getMessage()); }finally{ rs.close(); stmt.close(); con.close(); } %> </body> </html>
测试连接oracle数据库
<%@ page language="java" import="java.util.*" import="java.io.*" import="java.sql.*" pageEncoding="gbk"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>测试连接oracle数据库</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% Connection con=null; Statement stmt=null; ResultSet rs=null; try{ //加载MySql驱动程序 Class.forName("oracle.jdbc.driver.OracleDriver"); //连接数据库字符串 String dbUrl="jdbc:oracle:thin:@127.0.0.1:1521:fengniao"; String dbUser="system"; String dbPwd=""; //获得数据库连接 con=DriverManager.getConnection(dbUrl,dbUser,dbPwd); //得到命令对象 stmt=con.createStatement(); //查询得到结果集 rs=stmt.executeQuery("select books.*, books.rowid from books"); //输出查询结果 out.print("<table border=1 align='center'>"); out.print("<tr><td>编号</td><td>作者</td><td>书名</td><td>价格</td><td>出版时间</td><td>图书描述</td><td>数量</td></tr>"); while(rs.next()){ String col1=rs.getString(1); String col2=rs.getString(2); String col3=rs.getString(3); float col4=rs.getFloat(4); int col5=rs.getInt(5); String col6=rs.getString(6); int col7=rs.getInt(7); //打印获取到的数据 out.print("<tr><td>"+col1+"</td><td>"+col2+"</td><td>"+col3+"</td><td>"+col4+"</td><td>"+col5+"</td><td>"+col6+"</td><td>"+col7+"</td></tr>"); } out.print("</table>"); }catch(Exception e){ //捕获到的错误信息打印出来 out.print(e.getMessage()); }finally{ //rs.close(); stmt.close(); con.close(); } %> </body> </html>
ReSourceTest.jsp
<%@ page language="java" import="java.sql.*" import="javax.naming.*" import="javax.sql.*" contentType="text/html; charset=gbk"pageEncoding="gbk"%> <!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=gbk"> <title>Insert title here</title> </head> <body> <% Connection con=null; Statement stmt=null; ResultSet rs=null; try{ Context context=new InitialContext(); DataSource ds=(DataSource)context.lookup("java:comp/env/jdbc/bookdb"); //获得数据库连接 con=ds.getConnection(); //得到命令对象 stmt=con.createStatement(); //查询得到结果集 rs=stmt.executeQuery("select * from books"); //输出查询结果 out.print("<table border=1 align='center'>"); out.print("<tr><td>编号</td><td>作者</td><td>书名</td><td>价格</td><td>出版时间</td><td>图书描述</td><td>数量</td></tr>"); while(rs.next()){ String col1=rs.getString(1); String col2=rs.getString(2); String col3=rs.getString(3); float col4=rs.getFloat(4); int col5=rs.getInt(5); String col6=rs.getString(6); int col7=rs.getInt(7); //打印获取到的数据 out.print("<tr><td>"+col1+"</td><td>"+col2+"</td><td>"+col3+"</td><td>"+col4+"</td><td>"+col5+"</td><td>"+col6+"</td><td>"+col7+"</td></tr>"); } out.print("</table>"); }catch(Exception e){ //捕获到的错误信息打印出来 out.print(e.getMessage()); }finally{ //rs.close(); //stmt.close(); //con.close(); } %> </body> </html>
在apache-tomcat-6.0.18/conf/comtext.xml 中配置数据源
中加入
<Resource auth="Container" description="DB Connection" driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" maxPoolSize="100" minPoolSize="2" acquireIncrement="1" name="jdbc/Examination" user="sa" password="" factory="org.apache.naming.factory.BeanFactory" type="com.mchange.v2.c3p0.ComboPooledDataSource" jdbcUrl="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=bookdb" />
相关源码