日期:2014-05-16 浏览次数:20482 次
package com.boonya.h2.jdbc.connection; import java.sql.Connection; import java.sql.DriverManager; /** * DBConnection.java * 功能:测试H2提供的JDBC连接 * @author boonya * @version 1.0 2013-03-11 */ public class DBConnection { private static String url="jdbc:h2:~/test"; private static String driver="org.h2.Driver"; private static String username="sa"; private static String password="123456"; public static Connection getConnection(){ Connection conn=null; try{ Class.forName(driver); conn=DriverManager.getConnection(url,username,password); }catch(Exception e){ System.out.println("DBConnection create connection is failed:"); e.printStackTrace(); } return conn; } public static void main(String[] args) { System.out.println(DBConnection.getConnection());//打印:conn1: url=jdbc:h2:~/test user=SA } }
package com.boonya.h2.jdbc.conntion.pool; import java.sql.Connection; import java.sql.SQLException; import org.h2.jdbcx.JdbcConnectionPool; /** * DBConnectionPool.java * 功能:测试H2提供的JDBC连接池 * @author boonya * @version 1.0 2013-03-11 */ public class DBConnectionPool{ private static JdbcConnectionPool pool=JdbcConnectionPool.create("jdbc:h2:~/test", "sa", "123456"); static{ pool.setLoginTimeout(10000);//建立连接超时时间 pool.setMaxConnections(100);//建立连接最大个数 } public static JdbcConnectionPool getJDBCConnectionPool(){ return pool; } public static Connection getConnection(){ try { return pool.getConnection(); } catch (SQLException e) { System.out.println("DBConnectionPool create connection is failed:"); e.printStackTrace(); }finally{ pool.dispose(); //释放空闲连接 } return null; } public static void main(String[] args) { System.out.println("Timeout :"+DBConnectionPool.getJDBCConnectionPool().getLoginTimeout()+" ,connection count:"+DBConnectionPool.getJDBCConnectionPool().getMaxConnections()); System.out.println(DBConnectionPool.getConnection()); } }