简体   繁体   English

JDBC executeUpdate()仅插入1行

[英]JDBC executeUpdate() inserts only 1 row

My code reads data from a text file to populate a database. 我的代码从文本文件中读取数据以填充数据库。 The text file contains 5 lines, including a blank line (line 4). 文本文件包含5行,其中包括空行(第4行)。 The code reads the text file but only insert the last line in the database. 该代码读取文本文件,但仅在数据库中插入最后一行。 I also need the code to skip reading the first line in the text file. 我还需要代码来跳过阅读文本文件中的第一行。

Any help where I have gone wrong? 对我出问题的地方有帮助吗? I am new to Java Programming. 我是Java编程的新手。

Here is my code: 这是我的代码:

import java.io.*;
import java.sql.*;

public class Example {
   public static Connection getConnection() throws Exception {
        String driver = "org.apache.derby.jdbc.ClientDriver";
        String url = "jdbc:derby://localhost:1527/Orders";
        String username = "user";
        String password = "password";

        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
   } 
   public static void main(String[] args)throws Exception {

       String id ="";
       String firstName ="";
       String lastName ="";
       String street="";
       String city ="";
       String fileName = "src/Person.data"; 
       String line = null;


       PreparedStatement pstmt = null;
       Connection conn = null;

       try {
           FileReader fileReader = 
           new FileReader(fileName);
           BufferedReader bufferedReader = 
           new BufferedReader(fileReader);  


           while((line = bufferedReader.readLine()) != null) {

               if ( line.trim().length() == 0 ) {
                   continue;  // Skip blank lines
               } 
               String[] splited = line.split(",");
               //System.out.println(line);
               id=splited[0];
               firstName=splited[1];
               lastName=splited[2];
               street=splited[3];
               city=splited[4];
               System.out.println(line);
               System.out.println(splited[4]);

               conn = getConnection();
               conn.setAutoCommit(false);

               pstmt = conn.prepareStatement("insert into APP.Person(PERSON_ID, LAST_NAME, FIRST_NAME,STREET,CITY)values (?,?,?,?,?)");     
               pstmt.setString(1, id);
               pstmt.setString(2, firstName);
               pstmt.setString(3, lastName);
               pstmt.setString(4, street);
               pstmt.setString(5, city);
               pstmt.executeUpdate();
               conn.commit();
           }   
           bufferedReader.close();     
       }
       catch(FileNotFoundException ex) {
           System.out.println(
                "Unable to open file '" + 
                fileName + "'");  
        ex.printStackTrace();
       }
       catch(IOException ex) {
            System.out.println(
                "Error reading file '" 
                + fileName + "'");                  
       }

   }   
}

This is due to insertion only gets executed for the last record. 这是由于插入仅针对最后一条记录执行。 The better option is to use executebatch. 更好的选择是使用executebatch。 first add all those insert scripts in batch and after completion of loop just execute that batch. 首先批量添加所有这些插入脚本,然后在循环完成后执行该批处理。

Prepared statement is created inside the loop which is wrong. You need to create a Prepared statement out of the loop.

Use try catch block as follow: 使用try catch块,如下所示:

    try {
        FileReader fileReader =
                new FileReader(fileName);
        BufferedReader bufferedReader =
                new BufferedReader(fileReader);
        conn = getConnection();
            conn.setAutoCommit(false);

            pstmt = conn.prepareStatement("insert into APP.Person(PERSON_ID, LAST_NAME, FIRST_NAME,STREET,CITY)values (?,?,?,?,?)");

        while((line = bufferedReader.readLine()) != null) {

            if ( line.trim().length() == 0 ) {
                continue;  // Skip blank lines
            }
            String[] splited = line.split(",");
            //System.out.println(line);
            id=splited[0];
            firstName=splited[1];
            lastName=splited[2];
            street=splited[3];
            city=splited[4];
            System.out.println(line);
            System.out.println(splited[4]);


            pstmt.setString(1, id);
            pstmt.setString(2, firstName);
            pstmt.setString(3, lastName);
            pstmt.setString(4, street);
            pstmt.setString(5, city);
            pstmt.addBatch();

        }
        pstmt.executeBatch();
        conn.commit();
        bufferedReader.close();
    }
    catch(FileNotFoundException ex) {
        System.out.println(
                "Unable to open file '" +
                        fileName + "'");
        ex.printStackTrace();
    }
    catch(IOException ex) {
        System.out.println(
                "Error reading file '"
                        + fileName + "'");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM