Java(34)_ 用JDBC批量向数据库插入语句
package mysql; import java.sql.DriverManager; import java.sql.*; /** * 连接数据库,加载数据库。 * * */ public class MyTable { public static void main(String[] args) throws SQLException { String user = "fabu"; String password = "bowen@123"; String url = "jdbc:mysql://10.37.153.4:3306/rdrssit_4?user=fabu&password=bowen815@123&useSSL=false&useUnicode=true&characterEncoding=UTF8"; String driver = "com.mysql.jdbc.Driver"; PreparedStatement pst = null; Connection conn = null; try { // 1 加载驱动包 Class.forName(driver); conn = DriverManager.getConnection(url); if (!conn.isClosed()) { System.out.println("Success database connection! "); } else { System.out.println("Failed to connect to database"); } // 2 sql语句 String sql = "insert into test_table01 values(?, b'1', 11, 11, 11, 11, 11, '11.111111', '11.111111'," + " '11.111', '11.1111', '测试char_11', '测试varchar_11','2000-01-11', '2011', '2000-01-11 00:00:11', " + "'测试text_11', '00:00:11', '00:00:01.1', '00:00:00.011','00:00:00.000011', '2000-01-11 00:00:11', " + "'2000-01-11 00:00:01.1', '2000-01-11 00:00:00.011','2000-01-11 00:00:00.000011')"; pst = conn.prepareStatement(sql); // 3 执行sql语句,批量插入语句 for(int i=5;i<=100;i++){ pst.setInt(1,i); pst.executeUpdate(); } // pst.setInt(1,4); // pst.executeUpdate(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { // 6 关闭连接, pst.close();
conn.close();
} } }