赞
踩
数据源是数据库连接池里面的概念,连接池就是指当服务器启动时,先建立几个连接,在应用需要与数据库连接时,就从连接池里获取,使用完以后,不是将连接断掉,而是放回到池里面,这样就减少了数据连接创建的次数,大大提高了连接性能。而数据源就是给服务器一个配置信息,然服务器就知道怎么使用JDBC驱动,比如url参数,数据库实例名、用户名与密码等等。Java中的数据源就是javax.sql.DataSource。DataSource的创建可以有不同的实现,下面以mysql为例介绍几种常见DataSource的创建方法:
导入jar包:commons-dbcp.jar和commons-pool.jar和mysql-connector-java-5.1.39-bin.jar
推荐网址:
http://blog.csdn.net/peichuangaoling/article/details/45893391
http://blog.csdn.net/navy_xue/article/details/13090945
JNDI就是(JavaNaming and Directory Inteface)Java名称目录接口。
JNDI的作用:就是将资源引入到服务器中。可以将JNDI当成一个仓库。将Java对象放入到JNDI中去。
在java开发中,使用JDBC操作数据库的几个步骤:
1.使用Class.forName(类的全路径名称):用于加载数据库驱动程序。
2.获得数据库的Connection连接对象。DriverManager.getConnection()。
3.操作数据库:查询数据库,或者更新数据库内容,
4.关闭数据库连接:使用close方法。
注意:每次获取一个数据库连接的要经过这4个步骤,但是其中【1】,【2】,【4】是所有操作数据库的公共操作,只有【3】是操作数据库的不同步骤。并且获得数据库的connection对象和关闭数据库的连接都是要一定的时间。造成性能较差。
如果我们一开始就有已经创建好了多个connection对象,放在一个公共地方,当有一个连接数据库的请求,就从这个公共地方中取出一个connection,操作数据库,操作完成数据库,不关闭connection,而是放入到公共仓库中去,这就出现了数据库连接池的东西,就是存放多个Connection对象的地方。
如果需要配置全局的 Resource,则在server.xml的GlobalNamingResources节点里加入Resource,再在Context节点里加入ResourceLink的配置。
全局的resource只是为了重用,方便所有该tomcat下的web工程的数据源管理,但如果你的tomcat不会同时加载多个web工程,也就是说一个tomcat只加载一个web工程时,是没有必要配置全局的resource的。
每个web工程一个数据源:
在$CATALINA_HOME/conf/context.xml的根节点Context里加入Resource配置。这种配置方法,你在context.xml配置了一个数据源,但Tomcat中有同时运行着5个工程,那了就坏事儿了,这个在Tomcat启动时数据源被创建了5份,每个工程1份数据源。连接数会是你配置的参数的5倍。
只有在你的Tomcat只加载一个web工程时,才可以直接以context.xml配置数据源。
在Tomacat的conf/context.xml中的内容:
- <context>
- <WatchedResource>WEB-INF/web.xml</WatchedResource>
- <!-- 配置mysql数据库的连接池 -->
- <Resource name="jdbc/mysql"
- author="Container"
- type="javax.sql.DataSource"
- maxActive="4"
- maxIdle="2"
- maxWait="10000"
- username="root"
- password="root"
- driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://localhost:3306/mysqltest" />
- </context>
他只针对某一个Web项目的数据源的配置
1.导入要链接数据库的jar包文件。
例如sqlserver导入:sqljdbc4.jar包
Oracle导入:ojdbc14.jar包
配置Oracle数据库的JNDI数据源
- <Resource
-
- name="jdbc/oracle"
-
- auth="Container"
-
- type="javax.sql.DataSource"
-
- maxActive="100"
-
- maxIdle="30"
-
- maxWait="10000"
-
- username="lead_oams"
-
- password="p"
-
- driverClassName="oracle.jdbc.driver.OracleDriver"
-
- url="jdbc:oracle:thin:@192.168.1.229:1521:lead"/>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
配置SQLServer数据库的JNDI数据资源
- <Resource
-
- name="jdbc/sqlserver"
-
- auth="Container"
-
- type="javax.sql.DataSource"
-
- maxActive="100"
-
- maxIdle="30"
-
- maxWait="10000"
-
- username="sa"
-
- password="123456"
-
- driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
-
- url="jdbc:sqlserver://192.168.1.51:1433;DatabaseName=demo"/>
-
- </Context>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
MySQL导入:mysql-connector-java-5.0.8.jar包
配置MySQL数据库的JNDI数据资源:
- <Resource name="jdbc/mysql"
-
- author="Container"
-
- type="javax.sql.DataSource"
-
- maxActive="100"
-
- maxIdle="30"
-
- maxWait="10000"
-
- username="root"
-
- password="root"
-
- driverClassName="com.mysql.jdbc.Driver"
-
- url="jdbc:mysql://localhost:3306/mysqltest" />
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
2.在JNDI中配置数据库的连接池:
2):在web项目的META-INF下 建立context.xml文件
context.xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/mysql"
author="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mysqltest" />
</Context>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
说明:
上文中的设置的 maxActive="4"说明可以最大连接的个数为4个,再建立连接,则出现异常。
而maxIdle="2"说明当关闭数据库时(不是真正的断开连接,而是归还连接池中)连接池中最大可以有空闲的连接数为2个。
若是再有建立连接,此时若连接池中没有空闲的连接,但是又没有达到maxActive并发的最大连接数,则在连接池中建立连接。
3): 在Tomcat的conf/server.xml中配置:
找到:
配置
- <Resource name="jdbc/mysql"
- author="Container"
- type="javax.sql.DataSource"
- maxActive="100"
- maxIdle="30"
- maxWait="10000"
- username="root"
- password="liukunlun"
- driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://loaclhost:3306/fuxi" />
然后在Tomcat的conf/context.xml中配置 <ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSource"/> 如图:
上诉3个中法配置完成后最后都要配置web.xml内容 (在web应用中引用JNDI资源)
- <resource-ref>
- <!-- 对该资源的描述语言 -->
- <description> dbcpconnect</description>
- <!-- 引用的资源名,必须与context.xml中的名字一致 -->
- <res-ref-name> jdbc/mysql</res-ref-name>
- <!-- 资源类型 -->
- <res-type>javax.sql.DataSource</res-type>
- <!-- 管理权限 -->
- <res-auth>Container</res-auth>
- </resource-ref>
说明:
description :描述 (可随便写)
res-ref-name : java:/comp/env 下面的相关的名字
res-type : 资源的类型,资源管理器连接工厂的全限定名称。
res-auth : 资源需要的权限管理。 分两种:Application或 container
红色字体必须与java文件中的相一致
4):在Tomcat的conf/server.xml中配置虚拟目录时配置
在配置虚拟目录时,也就是在配置conf下面的server.xml时,在context标签内添加池配置.
在tomcat\conf下server.xml中找到
在其中添加:<Context path="/website" docBase="F:/JAVA/String/shujuyuan" reloadable="true">
如图:
注意:
docBase要改成你的项目目录。
path为虚拟路径,访问时的路径,注意:一定要加“/”
配置虚拟目录
在context 节点下配置
- <Resource
- name="jdbc/mysql"
- author="Container"
- type="javax.sql.DataSource"
- maxActive="100"
- maxIdle="30"
- maxWait="10000"
- username="root"
- password="liukunlun"
- driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://loaclhost:3306/fuxi" />
如图:
配置好后只需重启服务器,无需在web.xml文件中配置 。
参数设置:
name 表示指定的jdbc,jndi名称 ,通常采用jdbc/**方式
auth 管理权限,指定管理Resource的Manager,可以是Container或application(俩者固定使用)
type 指出Resource所属的类名,是什么类型的数据源,使用标准的javax.sql.DataSource
maxActive 默认值是 8, 连接池中同时可以分派的最大活跃连接数,设为0表示无限制
maxIdle 连接池中最多可空闲的连接数 默认是 8
maxWait 为连接最大的等待时间,单位毫秒,如果超过此时间将接到异常。设为-1表示无限制
username 表示数据库用户名
password 表示数据库用户的密码
driverClassName 数据库驱动
url 数据库连接url
testWhileIdle : 默认值是 false, 当连接池中的空闲连接是否有效
<!-- (红色字体为常用参数设置)-->
logAbandoned 表示被丢弃的数据库连接是否做记录,以便跟踪
removeAbandonedTimeout : 默认值是 300( 秒), 活动连接的最大空闲时间
removeAbandoned : 默认值是 false, 是否清理 removeAbandonedTimeout 秒没有使用的活动连接 , 清理后并没有放回连接池
logAbandoned : 默认值 false, 连接池收回空闲的活动连接时是否打印消息
testOnBorrow : 默认值是 true ,当从连接池取连接时,验证这个连接是否有效
testOnReturn : 默认值是 flase, 当从把该连接放回到连接池的时,验证这个连接是否有效
timeBetweenEvictionRunsMilis : 默认值是 -1 ,单位是毫秒,每隔一段多少毫秒跑一次回收空闲线程的线程
minEvictableIdleTimeMilis : 默认值是 1000 * 60 * 30(30 分钟 ) ,单位毫秒,连接池中连接可空闲的时间
numTestsPerEvictionRun : 默认值是 3 ,每次验证空闲连接的连接数目
connectionInitSqls : 默认值是 null, 一组用来初始化连接的 sql 语句,这些语句只在连接工厂创建连接时执行一次。
validationQuery : 一条 sql 语句,用来验证数据库连接是否正常。这条语句必须是一个查询模式,并至少返回一条数据。一般用“ select 1 ”
initialSize : 默认值是 0, 连接池创建连接的初始连接数目
minIdle : 默认是 0, 连接数中最小空闲连接数
注意:
minEvictableIdleTimeMilis和removeAbandonedTimeout参数,这两个参数针对的连接对象不一样,
minEvictableIdleTimeMillis 针对连接池中的连接对象 ,
removeAbandonedTimeout 针对未被 close 的活动连接 (被调用,不在池中的连接对象 )
WEB工程中的使用
创建数据源:
正确的配置后,就可以在程序中以JNDI的方式创建数据源,得到数据库连接并进行相应的操作。代码如下:
DataSourceManager类
- package utils;
-
- import java.sql.Connection;
-
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import javax.sql.DataSource;
-
- public class DataSourceManager {
- private static Context context;
- private static DataSource dataSource;
-
- static {
- try {
- // 实例上下文目录
- context = new InitialContext();
-
- // 在命名空间和目录空间中查找 数据源名称 返回数据库连接池对象 JNDI
- dataSource=(DataSource)context.lookup("java:comp/env/jdbc/mysql");
- } catch (NamingException e) {
- e.printStackTrace();
- }
-
- }
-
- public static Connection getConnection() {
- Connection conn = null;
- try {
- conn = dataSource.getConnection();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return conn;
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
JSP中使用
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <%@page import="java.sql.*, javax.sql.*, javax.naming.*"%>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>数据</title>
- </head>
- <body>
- <h1>Using a DataSource</h1>
- <%
- DataSource ds = null;
- Connection conn = null;
- ResultSet result = null;
- Statement stmt = null;
- ResultSetMetaData rsmd = null;
- try{
- Context context = new InitialContext();
- Context envCtx = (Context)
- context.lookup("java:comp/env");
- ds = (DataSource)envCtx.lookup("jdbc/mysql");
- if (ds != null) {
- conn = ds.getConnection();
- stmt = conn.createStatement();
- result = stmt.executeQuery("SELECT * FROM student");
- }
-
- }
- catch (Exception e) {
- System.out.println("Error occurred " + e);
- }
- int columns=0;
- try {
- rsmd = result.getMetaData();
- columns = rsmd.getColumnCount();
- }
- catch (Exception e) {
- System.out.println("Error occurred " + e);
- }
- System.out.println(columns);
- %>
- <table width="90%" border="1">
- <tr>
-
- <% // write out the header cells containing the column labels
- try {
- for (int i=1; i<=columns; i++) {
- out.write("<th>" + rsmd.getColumnLabel(i) "</th>");
- }
- %>
- </tr>
- <%
- // now write out one row for each entry in the database table
- while (result.next()) {
- out.write("<tr>");
- for (int i=1; i<=columns; i++) {
- out.write("<td>" + result.getString(i) + "</td>");
- }
- out.write("</tr>");
- }
-
- // close the connection, resultset, and the statement
- result.close();
- stmt.close();
- conn.close(); // 将连接重新放回到池中
- } catch (Exception e) { // end of the try block
- System.out.println("Error " + e);
- }finally { // ensure everything is closed
- try {
- if (stmt != null)
- stmt.close();
- } catch (Exception e) {}
- try {
- if (conn != null)
- conn.close();
- } catch (Exception e) {}
- }
-
- %>
- </table>
- </body>
- </html>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Dao类中的使用
DBCP(DataBase connection pool),数据库连接池。是 apache上的一个Java 连接池项目,也是 tomcat使用的连接池组件。
单独使用dbcp需要3个包:common-dbcp.jar,common-pool.jar,common-collections.jar
由于建立数据库连接是一个非常耗时耗资源的行为,所以通过连接池预先同数据库建立一些连接,放在内存中,应用程序需要建立数据库连接时直接到连接池中申请一个就行,用
完后再放回去。
获取数据库连接的类:DBCP.java
- import java.sql.Connection;
- import javax.sql.DataSource;
- import org.apache.commons.dbcp2.BasicDataSource;
- public class DBCP {
- private static BasicDataSource basicDataSource = null ;
- private static DataSource dataSource = null;
- static{
- basicDataSource = new BasicDataSource();
- basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
- basicDataSource.setUrl("jdbc:mysql://localhost:3306/fuxi");
- basicDataSource.setUsername("root");
- basicDataSource.setPassword("liukunlun");
- basicDataSource.setLogAbandoned(true);
- basicDataSource.setMaxIdle(30);
- basicDataSource.setInitialSize(50);
- dataSource = basicDataSource;
- }
- public static Connection getConnection() {
- Connection conn = null;
- try {
- conn = dataSource.getConnection();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return conn;
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
配置文件的 DBCO连接池的配置
连接数据库信息的配置文件:dbcp.properties
dbcp.driverClassName=com.mysql.jdbc.Driver
dbcp.url=jdbc:mysql://localhost:3306/fuxi
dbcp.username=root
dbcp.password=liukunlun
dbcp.initialSize=30
dbcp.minIdle=10
dbcp.maxIdle=10
dbcp.maxWait=1000
dbcp.maxActive=30
获取数据库连接池的类
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.Properties;
-
- import javax.sql.DataSource;
- import org.apache.commons.dbcp.BasicDataSource;
- public class DbcpConnection {
- private static DataSource dataSource;
- private static Connection connection;
-
- public static void initDataSource(){
- FileInputStream is = null;
- Properties properties = new Properties();
-
- String driverClassName = null;
- String url = null;
- String username = null;
- String password = null;
-
- int initialSize = 0;
- int minIdle = 0;
- int maxIdle = 0;
- int maxWait = 0;
- int maxActive = 0;
-
- try {
- String path = System.getProperty("user.dir")+"\\src\\com\\xiami\\db\\connection\\";
- is = new FileInputStream(path+"dbcp.properties");
- properties.load(is);
-
- driverClassName = properties.getProperty("dbcp.driverClassName");
- url = properties.getProperty("dbcp.url");
- username = properties.getProperty("dbcp.username");
- password = properties.getProperty("dbcp.password");
- initialSize = Integer.parseInt((properties.getProperty("dbcp.initialSize").trim()));
- minIdle = Integer.parseInt((properties.getProperty("dbcp.minIdle")).trim());
- maxIdle = Integer.parseInt((properties.getProperty("dbcp.maxIdle")).trim());
- maxWait = Integer.parseInt((properties.getProperty("dbcp.maxWait")).trim());
- maxActive = Integer.parseInt((properties.getProperty("dbcp.maxActive")).trim());
-
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException ioe){
- ioe.printStackTrace();
- }finally{
- try {
- is.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- BasicDataSource bds = new BasicDataSource();
-
- bds.setUrl(url);
- bds.setDriverClassName(driverClassName);
- bds.setUsername(username);
- bds.setPassword(password);
- bds.setInitialSize(initialSize);
- bds.setMaxActive(maxActive);
- bds.setMinIdle(minIdle);
- bds.setMaxIdle(maxIdle);
- bds.setMaxWait(maxWait);
-
- dataSource = bds;
- }
-
- public static Connection getConnection() throws SQLException {
- if (dataSource == null) {
- initDataSource();
- }
- Connection conn = null;
- if (dataSource != null) {
- conn = dataSource.getConnection();
- }
- return conn;
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
在Service层使用的时候:ZhiDianJieDu.java (业务逻辑处理的类)
- package utils;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.Statement;
- public class Duquxinxi{
- private Statement stmt = null;
- private ResultSet result = null;
- private ResultSetMetaData rsmd = null;
- private Connection conn = null;
- public void test() {
- try {
- conn = DBCP.getConnection();
- System.out.println("连接成功!");
- stmt = conn.createStatement();
- result = stmt.executeQuery("SELECT * FROM fuxi");
- } catch (Exception e) {
- System.out.println(e);
- }
- int columns = 0;
- try {
- rsmd = result.getMetaData();
- columns = rsmd.getColumnCount();
- } catch (Exception e) {
- System.out.println("Error occurred " + e);
- }
- try {
- for (int i = 1; i <= columns; i++) {
- System.out.println(rsmd.getColumnLabel(i));
- }
- while (result.next()) {
- for (int i = 1; i <= columns; i++) {
- System.out.println(result.getString(i));
- }
- System.out.println();
- }
- // close the connection, resultset, and the statement
- // 关闭连接,resultset,声明
- result.close();
- stmt.close();
- conn.close();
- } // end of the try block
- catch (Exception e) {
- System.out.println("Error " + e);
- }
- // ensure everything is closed 确保一切都是封闭的
- finally {
- try {
- if (stmt != null)
- stmt.close();
- } catch (Exception e) {
- }
- try {
- if (conn != null)
- conn.close();
- } catch (Exception e) {
- }
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
C3p0.properties 位置
内容
#jdbc基本信息
driverClass=org.gjt.mm.mysql.Driver
jdbcUrl=jdbc:mysql://localhost:3306/fuxi
user=root
password=liukunlun
#c3p0连接池信息
c3p0.minPoolSize=3
c3p0.maxPoolSize=25
#当连接池中的连接耗尽的时候c3p0一次同时获取的连接数
c3p0.acquireIncrement=3
#定义在从数据库获取新连接失败后重复尝试的次数
c3p0.acquireRetryAttempts=60
#两次连接中间隔时间,单位毫秒
c3p0.acquireRetryDelay=1000
#连接关闭时默认将所有未提交的操作回滚
c3p0.autoCommitOnClose=false
#当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出#SQLException,如设为0则无限期等待。单位毫秒
c3p0.checkoutTimeout=3000
#每120秒检查所有连接池中的空闲连接。Default: 0
c3p0.idleConnectionTestPeriod=120
#最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0
c3p0.maxIdleTime=600
#如果设为true那么在取得连接的同时将校验连接的有效性。Default: false
c3p0.testConnectionOnCheckin=true
#c3p0将建一张名为c3p0TestTable的空表,并使用其自带的查询语句进行测试。
jdbc.automaticTestTable = c3p0TestTable
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
C3P0ConnentionProvider.Java
- package utils;
- import java.io.FileInputStream;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.Properties;
-
- import javax.sql.DataSource;
-
- import com.mchange.v2.c3p0.DataSources;
-
- /**
- * c3p0连接池管理类
- */
- public class C3P0ConnentionProvider {
-
- private static final String JDBC_DRIVER = "driverClass";
- private static final String JDBC_URL = "jdbcUrl";
-
- private static DataSource ds;
- /**
- * 初始化连接池代码块
- */
- static {
- initDBSource();
- }
-
- /**
- * 初始化c3p0连接池
- */
- private static final void initDBSource() {
- Properties c3p0Pro = new Properties();
- try {
- // 加载配置文件
- String path = C3P0ConnentionProvider.class.getResource("/").getPath();
- String websiteURL = (path.replace("/build/classes", "").replace("%20"," ").replace("classes/", "") + "c3p0.properties").replaceFirst("/", "");
- FileInputStream in = new FileInputStream(websiteURL);
- c3p0Pro.load(in);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- String drverClass = c3p0Pro.getProperty(JDBC_DRIVER);
- if (drverClass != null) {
- try {
- // 加载驱动类
- Class.forName(drverClass);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
-
- }
-
- Properties jdbcpropes = new Properties();
- Properties c3propes = new Properties();
- for (Object key : c3p0Pro.keySet()) {
- String skey = (String) key;
- if (skey.startsWith("c3p0.")) {
- c3propes.put(skey, c3p0Pro.getProperty(skey));
- } else {
- jdbcpropes.put(skey, c3p0Pro.getProperty(skey));
- }
- }
-
- try {
- // 建立连接池
- DataSource unPooled = DataSources.unpooledDataSource(c3p0Pro.getProperty(JDBC_URL), jdbcpropes);
- ds = DataSources.pooledDataSource(unPooled, c3propes);
-
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 获取数据库连接对象
- *
- * @return 数据连接对象
- * @throws SQLException
- */
- public static synchronized Connection getConnection() throws SQLException {
- final Connection conn = ds.getConnection();
- conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
- return conn;
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
使用
datasource2.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <%@page import="utils.*"%>
- <%@page import="java.sql.*, javax.sql.*, javax.naming.*"%>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <%
- Statement stmt = null;
- ResultSet result = null;
- ResultSetMetaData rsmd = null;
- Connection conn = null;
- try{
-
-
- conn = C3P0ConnentionProvider.getConnection();
- //conn = DataSourceManager.getConnection();
- out.println("连接成功!");
- stmt = conn.createStatement();
- result = stmt.executeQuery("SELECT * FROM fuxi");
- } catch (Exception e) {
- out.println(e);
- }
- int columns=0;
- try {
- rsmd = result.getMetaData();
- columns = rsmd.getColumnCount();
- }
- catch (Exception e) {
- System.out.println("Error occurred " + e);
- }
- %>
- <table width="90%" border="1">
- <tr>
- <% // write out the header cells containing the column labels 写出标题单元格包含列标签
- try {
- for (int i=1; i<=columns; i++) {
- out.write("<th>" + rsmd.getColumnLabel(i) + "</th>");
- }
- %>
- </tr>
- <% // now write out one row for each entry in the database table 现在写出一行中的每个条目数据库表中
- while (result.next()) {
- out.write("<tr>");
- for (int i=1; i<=columns; i++) {
- out.write("<td>" + result.getString(i) + "</td>");
- }
- out.write("</tr>");
- }
-
- // close the connection, resultset, and the statement 关闭连接,resultset,声明
- result.close();
- stmt.close();
- //conn.close();
- } // end of the try block
- catch (Exception e) {
- System.out.println("Error " + e);
- }
- // ensure everything is closed 确保一切都是封闭的
- finally {
- try {
- if (stmt != null)
- stmt.close();
- } catch (Exception e) {}
- try {
- // if (conn != null)
- // conn.close();
- } catch (Exception e) {}
- }
-
- %>
- </table>
- </body>
- </html>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。