zoukankan      html  css  js  c++  java
  • 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();
    } } }
  • 相关阅读:
    34. Find First and Last Position of Element in Sorted Array
    42. Trapping Rain Water
    HDU-2952 Counting Sheep (DFS)
    HDU-1518 Square(DFS)
    HDU-1253 胜利大逃亡 (BFS)
    HDU-1026 Ignatius and the Princess I (BFS)
    最小生成树之Prim算法,Kruskal算法
    HDU-1495 非常可乐(BFS)
    strncpy 用法
    字符串函数总结
  • 原文地址:https://www.cnblogs.com/sunnybowen/p/9879532.html
Copyright © 2011-2022 走看看