标签:
实际开发中需要向数据库发送多条SQL语句,这时,如果逐条执行SQL语句,效率会很低,因此可以使用JDBC提供的批处理机制。Statement和PreparedStatemen都实现了批处理。测试表结构如下:
Statement批处理程序示例
1 package server; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.Statement; 7 8 import com.mysql.jdbc.PreparedStatement; 9 10 public class DemoJDBC { 11 public static void main(String[] args) throws Exception { 12 // 加载驱动类 13 Class.forName("com.mysql.jdbc.Driver"); 14 15 // 通过DriverManager获取数据库连接 16 String url = "jdbc:mysql://192.168.1.150/test"; 17 String user = "teamtalk"; 18 String password = "123456"; 19 Connection connection = (Connection) DriverManager.getConnection( 20 url, user, password); 21 22 String sql1 = "DROP TABLE IF EXISTS people"; 23 String sql2 = "CREATE TABLE people(id int, name varchar(20))"; 24 String sql3 = "INSERT people VALUES(2, ‘hdu‘)"; 25 String sql4 = "UPDATE people SET id = 1"; 26 Statement statement = (Statement) connection.createStatement(); 27 statement.addBatch(sql1); 28 statement.addBatch(sql2); 29 statement.addBatch(sql3); 30 statement.addBatch(sql4); 31 statement.executeBatch(); 32 33 ResultSet resultSet = statement.executeQuery("SELECT * from people"); 34 while (resultSet.next()) { 35 System.out.println(resultSet.getString(1) + " " + resultSet.getString(2)); 36 } 37 } 38 }
PreparedStatement批处理
1 package server; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.Statement; 7 8 import com.mysql.jdbc.PreparedStatement; 9 10 public class DemoJDBC { 11 public static void main(String[] args) throws Exception { 12 // 加载驱动类 13 Class.forName("com.mysql.jdbc.Driver"); 14 15 // 通过DriverManager获取数据库连接 16 String url = "jdbc:mysql://192.168.1.150/test"; 17 String user = "teamtalk"; 18 String password = "123456"; 19 Connection connection = (Connection) DriverManager.getConnection( 20 url, user, password); 21 22 PreparedStatement statement = (PreparedStatement) connection.prepareStatement("INSERT people VALUES(?,?)"); 23 for (int i = 1; i < 4; i++) { 24 statement.setInt(1, i); 25 statement.setString(2, "hdu" + i); 26 statement.addBatch(); 27 } 28 statement.executeBatch(); 29 30 ResultSet resultSet = statement.executeQuery("SELECT * from people"); 31 while (resultSet.next()) { 32 System.out.println(resultSet.getString(1) + " " + resultSet.getString(2)); 33 } 34 } 35 }
针对JDBC处理事务的操作,在Connection接口中,提供了3个相关的方法,具体如下:
1 setAutoCommit(boolean autoCommit); // 设置是否自动提交事务 2 commit(); // 提交事务 3 rollback(); // 撤销事务
将setAutoCommit()方法参数设置为false后,事务必须使用conn.commit()方法提交,而事务回滚不一定显式执行conn.rollback()。如果程序最后没有执行conn.commit(),事务也会回滚,一般是直接抛出异常,终止程序的正常执行。因此,通常情况下,会conn.rollback()语句放在catch语句块执行。
1 package demo.jdbc; 2 3 import java.sql.DriverManager; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 7 import com.mysql.jdbc.Connection; 8 import com.mysql.jdbc.PreparedStatement; 9 import com.mysql.jdbc.Statement; 10 11 public class FirstJDBC { 12 public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException { 13 Connection connection = null; 14 try { 15 // 加载驱动类 16 Class.forName("com.mysql.jdbc.Driver"); 17 18 // 通过DriverManager获取数据库连接 19 String url = "jdbc:mysql://192.168.1.150/test"; 20 String user = "teamtalk"; 21 String password = "123456"; 22 connection = (Connection) DriverManager.getConnection( 23 url, user, password); 24 // 关闭事务的自动提交 25 connection.setAutoCommit(false); 26 27 Statement statement = (Statement) connection.createStatement(); 28 PreparedStatement statement1 = (PreparedStatement) connection.prepareStatement("INSERT people VALUES(?, ?)"); 29 PreparedStatement statement2 = (PreparedStatement) connection.prepareStatement("INSERT people VALUES(?, ?)"); 30 31 statement1.setInt(1, 1); 32 statement1.setString(2, "hdu1"); 33 statement2.setInt(1, 2); 34 statement2.setString(2, "hdu2"); 35 36 statement1.executeUpdate(); 37 statement2.executeUpdate(); 38 39 ResultSet resultSet = statement.executeQuery("SELECT * from people"); 40 while (resultSet.next()) { 41 System.out.println(resultSet.getString(1) + " " + resultSet.getString(2)); 42 } 43 } 44 catch (Exception e) { 45 // 回滚事务 46 connection.rollback(); 47 e.printStackTrace(); 48 } 49 } 50 }
使用DBCP数据源需要使用3个jar包,分别是commons-dbcp.jar包(https://commons.apache.org/proper/commons-dbcp/download_dbcp.cgi)、commons-pool.jar(http://commons.apache.org/proper/commons-pool/download_pool.cgi)包和commons-logging.jar(http://commons.apache.org/proper/commons-logging/download_logging.cgi)包。以下程序示例是通过BasicDataSource类直接创建数据源对象。
1 package server; 2 3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 import javax.sql.DataSource; 9 10 import org.apache.commons.dbcp2.BasicDataSource; 11 12 //import com.mysql.jdbc.Statement; 13 14 public class DemoDBCP { 15 public static DataSource ds = null; 16 17 static { 18 // 获取DBCP数据源实现类 19 BasicDataSource bds = new BasicDataSource(); 20 // 设置连接池配置信息 21 bds.setDriverClassName("com.mysql.jdbc.Driver"); 22 bds.setUrl("jdbc:mysql://192.168.1.150/test"); 23 bds.setUsername("teamtalk"); 24 bds.setPassword("123456"); 25 // 设置连接池参数 26 bds.setInitialSize(5); 27 bds.setMaxTotal(5); 28 ds = bds; 29 } 30 31 public static void main(String[] args) throws SQLException { 32 Connection connection = (Connection) ds.getConnection(); 33 java.sql.DatabaseMetaData metaData = connection.getMetaData(); 34 35 System.out.println(metaData.getURL()); 36 System.out.println(metaData.getUserName()); 37 System.out.println(metaData.getDriverName()); 38 39 Statement statement = (Statement) connection.createStatement(); 40 ResultSet resultSet = statement.executeQuery("SELECT * from people"); 41 while (resultSet.next()) { 42 System.out.println(resultSet.getString(1) + " " + resultSet.getString(2)); 43 } 44 } 45 }
c3p0是目前最流行的开源数据库连接池之一,它实现了DataSource数据源接口,支持JDBC2和JDB3的标准规范,易于扩展并且性能优越,著名的开源框架Hibernate和Spring使用功能的都是该数据源。c3p0连接数据库示例(通过配置文件方式),注意:配置文件名称必须是c3p0-config.xml,该文件必须放在工程bin目录下。下载地址:https://sourceforge.net/projects/c3p0/?source=typ_redirect
配置文件c3p0-config.xml为:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <c3p0-config> 3 <default-config> 4 <property name="jdbcUrl">jdbc:mysql://192.168.1.150/test</property> 5 <property name="driverClass">com.mysql.jdbc.Driver</property> 6 <property name="user">teamtalk</property> 7 <property name="password">123456</property> 8 9 <property name="checkoutTimeout">3000</property> 10 <property name="idleConnectionTestPeriod">30</property> 11 <property name="initialPoolSize">10</property> 12 <property name="maxIdleTime">30</property> 13 <property name="maxPoolSize">100</property> 14 <property name="minPoolSize">10</property> 15 <property name="maxStatements">200</property> 16 </default-config> 17 18 <named-config name="demo"> 19 <property name="jdbcUrl">jdbc:mysql://192.168.1.150/test</property> 20 <property name="driverClass">com.mysql.jdbc.Driver</property> 21 <property name="user">teamtalk</property> 22 <property name="password">123456</property> 23 24 <property name="checkoutTimeout">3000</property> 25 <property name="idleConnectionTestPeriod">30</property> 26 <property name="initialPoolSize">10</property> 27 <property name="maxIdleTime">30</property> 28 <property name="maxPoolSize">100</property> 29 <property name="minPoolSize">10</property> 30 <property name="maxStatements">200</property> 31 </named-config> 32 33 </c3p0-config>
1 package server; 2 3 import java.sql.Connection; 4 import java.sql.SQLException; 5 6 import javax.sql.DataSource; 7 8 import com.mchange.v2.c3p0.ComboPooledDataSource; 9 10 public class DemoDBCP { 11 public static DataSource ds = null; 12 13 static { 14 ComboPooledDataSource cpds = new ComboPooledDataSource(); 15 ds = cpds; 16 } 17 18 public static void main(String[] args) throws SQLException { 19 Connection connection = (Connection) ds.getConnection(); 20 java.sql.DatabaseMetaData metaData = connection.getMetaData(); 21 22 System.out.println(metaData.getURL()); 23 System.out.println(metaData.getUserName()); 24 System.out.println(metaData.getDriverName()); 25 } 26 }
标签:
原文地址:http://www.cnblogs.com/luoxn28/p/5277596.html