传统体式格局和衔接池体式格局
传统体式格局的步骤
运用传统体式格局在Java中运用JDBC衔接数据库,完成一次数据库的操纵,平常有以下几个步骤:
1. 加载驱动。
2. 竖立衔接。
3. 实行SQL语句。
4. 开释衔接。
5. 传统体式格局的弊病
每一次对数据库的操纵都要竖立一次衔接,而且会将获得的Connection对象加载到内存中,致使斲丧了大批的内存和时候。假如短时候有许多须要举行竖立衔接的操纵,会致使占用许多系统资本,甚至会致使服务器崩溃。
同竖立衔接相对应,每次运用都须要手动开释衔接,假如遗忘开释衔接或许顺序出现异常未能胜利开释,会致使内存泄漏。
另外,传统体式格局并不能掌握衔接的数目,假如衔接的人数过量,会致使无限制的建立衔接对象,致使内存开支过大,服务器崩溃。
(引荐视频:java视频教程)
衔接池的步骤
1. 建立衔接池并设置衔接属性。
2. 运用衔接池猎取衔接。
衔接池的上风
每次须要衔接数据库时,不须要竖立衔接,而是经由过程衔接池猎取,由衔接池供应衔接。
在运用完衔接后,不须要手动开释衔接,而是交由衔接池开释衔接。
能够经由过程衔接池掌握衔接的数目,在衔接池里的衔接可屡次重复运用,避免了无限制建立衔接的题目。
运用衔接池
运用C3P0数据库衔接池
导入jar包:
c3p0-0.9.5.2.jar
在当前项目标代码根目录 src 下新建名为 c3p0-config.xml 的设置文件,注重文件称号不可转变,内容以下:
<c3p0-config> <!-- 衔接称号 --> <named-config name="mysql"> <!-- 接数据库的驱动类名 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <!-- 衔接属性 --> <property name="jdbcUrl">jdbc:mysql://192.168.35.128:3306/demo</property> <property name="user">root</property> <property name="password">123456</property> <!-- 当衔接池用完时守候猎取新衔接的时候,超时后将抛出SQLException,单元毫秒,如设为0则无限日守候。默以为0。 --> <property name="checkoutTimeout">5000</property> <!-- 当衔接用尽后,一次猎取的衔接个数 --> <property name="acquireIncrement">2</property> <!-- 初始衔接数 --> <property name="initialPoolSize">1</property> <!-- 最小衔接数 --> <property name="minPoolSize">3</property> <!-- 最大衔接数 --> <property name="maxPoolSize">5</property> </named-config> </c3p0-config>
顺序代码:
public class TestDataPool { // 依据设置文件里的称号建立衔接池 public static ComboPooledDataSource cpds = new ComboPooledDataSource("mysql"); /** * 主顺序 */ public static void main(String[] args) { // 模仿屡次对数据库的查询操纵 for (int i = 0; i < 6; i++) { new Thread(new Runnable() { @Override public void run() { select(); } }, "线程" + i).start(); } } /** * 查询顺序 */ public static void select() { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; // 猎取衔接并实行SQL try { conn = cpds.getConnection(); pstmt = conn.prepareStatement("select * from student where id = 906"); rs = pstmt.executeQuery(); while (rs.next()) { System.out.println(Thread.currentThread().getName() + "\t" + rs.getString(1) + "\t" + rs.getString(2) + "\t" + rs.getString("address")); } } catch (Exception e) { e.printStackTrace(); } finally { // 开释资本 try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
运用DBCP数据库衔接池
导入jar包:
commons-dbcp-1.4.jar2 commons-pool-1.5.5.jar
在当前项目标代码根目录 src 下新建名为 dbcp.properties 的设置文件,文件名须要同代码中援用的文件名一致,内容以下:
# 接数据库的驱动类名 driverClassName=com.mysql.jdbc.Driver # 衔接属性 url=jdbc:mysql://192.168.35.128:3306/demo username=root password=123456 # 初始化衔接数 initialSize=10 # 最大衔接数 maxActive=15
顺序代码:
public class TestDBCP { // 依据设置文件里的称号建立衔接池 private static DataSource source = null; static { Properties pros = new Properties(); InputStream is = TestDBCP.class.getClassLoader().getResourceAsStream("dbcp.properties"); try { pros.load(is); source = BasicDataSourceFactory.createDataSource(pros); } catch (Exception e) { e.printStackTrace(); } } /** * 主顺序 */ public static void main(String[] args) { // 模仿屡次对数据库的查询操纵 for (int i = 0; i < 6; i++) { new Thread(new Runnable() { @Override public void run() { select(); } }, "线程" + i).start(); } } /** * 查询顺序 */ public static void select() { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; // 猎取衔接并实行SQL try { conn = source.getConnection(); pstmt = conn.prepareStatement("select * from student where id = 906"); rs = pstmt.executeQuery(); while (rs.next()) { System.out.println(Thread.currentThread().getName() + "\t" + rs.getString(1) + "\t" + rs.getString(2) + "\t" + rs.getString("address")); } } catch (Exception e) { e.printStackTrace(); } finally { // 开释资本 try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
以上就是Java操纵数据库——运用衔接池衔接数据库的细致内容,更多请关注ki4网别的相干文章!