【问题标题】:Generate multiple insert queries via java通过java生成多个插入查询
【发布时间】:2019-09-06 02:48:15
【问题描述】:

我有这个查询 "插入 pro_vendor (vendor_name,super_category,sub_category,created_by,vowes,wowes,created_date,status) 值 ('V','Sup','Sub','user' ,'0','0',CURRENT_TIMESTAMP'active')".

如果我需要处理结果集,我通常通过准备好的语句或简单地通过 statement.executeQuery(query) 来实现单行。

现在问题是我有一个数组,其中第一个参数即 vendor_name 有多个(可能是 1,可能是 2,可能是 50)不同的替代方案,所以我想使用循环生成插入查询。

我无法使用准备好的语句,因为我无法动态生成准备好的语句对象,executeQuery 也一样,因为我无法动态生成语句对象。

我所要求的只是一种根据我的数组大小生成插入查询的方法,谢谢。

【问题讨论】:

  • 使用准备好的语句
  • 看起来很简单,但是如何根据数组的大小动态创建新的准备好的语句对象??
  • 要么你的解释很糟糕,要么你迷路了。如果要插入多个 pro_vendor 记录,则创建一个 PreparedStatement 并循环设置值,例如ps.SetString (1, arr[0].getVendorName());

标签: java mysql jsp servlets jdbc


【解决方案1】:

我认为要插入多条记录,你必须使用批量插入。

Connection conn;
        try {
            conn = DriverManager.getConnection("connection parameter's");

            Statement stmt = conn.createStatement();  // establish connection and make a table for the data.


            // COPY statement
            conn.setAutoCommit(false);


            // Drop table and recreate.
            stmt.execute("DROP TABLE IF EXISTS customers CASCADE");
            stmt.execute("CREATE TABLE customers (CustID int, Last_Name  char(50), First_Name char(50),Email char(50), "
                            + "Phone_Number char(12))");

            // Some dummy data to insert. 
            String[] firstNames = new String[] { "Anna", "Bill", "Cindy","Don", "Eric" };
            String[] lastNames = new String[] { "Allen", "Brown", "Chu", "Dodd", "Estavez" };
            String[] emails = new String[] { "aang@example.com", "b.brown@example.com", "cindy@example.com","d.d@example.com", "e.estavez@example.com" };
            String[] phoneNumbers = new String[] { "123-456-7890", "555-444-3333", "555-867-5309", "555-555-1212", "781-555-0000" };
            // Create the prepared statement
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO customers (CustID, Last_Name, " + 
                            "First_Name, Email, Phone_Number)  VALUES(?,?,?,?,?)");

            // Add rows to a batch in a loop. Each iteration adds a
            for (int i = 0; i < firstNames.length; i++) {

                pstmt.setInt(1, i + 1);
                pstmt.setString(2, lastNames[i]);
                pstmt.setString(3, firstNames[i]);
                pstmt.setString(4, emails[i]);
                pstmt.setString(5, phoneNumbers[i]);
                pstmt.addBatch();  // Add row to the batch.
            }

            try {
                // Batch is ready, execute it to insert the data
                pstmt.executeBatch();
            } catch (SQLException e) {
                System.out.println("Error message: " + e.getMessage());
                return; // Exit if there was an error
            }

            // Commit the transaction to close the COPY command
            conn.commit();
            conn.close();

【讨论】:

  • 你必须使用批量插入。 - 不必,但一个好主意
  • @ScaryWombat 所以你能建议一些没有批量插入的东西吗?只是问一下。
  • 是的,执行上述操作,但不添加到批处理中,auto-commit = true,或者在您的代码中提交。
  • @Dipak Aher 谢谢bhai,这个答案帮助了我!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 2011-03-22
  • 2013-11-06
  • 1970-01-01
  • 2021-06-25
  • 1970-01-01
  • 2018-04-26
相关资源
最近更新 更多