如何从CSV文件中获取数据并将其插入数据库?

时间:2019-11-14 10:04:01

标签: java oracle jdbc

我正在尝试将CS​​V文件中的数据插入Oracle数据库。

我不确定我的问题在哪里,因为它一直在循环。

到目前为止,我的代码:


  public static void main(String[] args){

    if(args.length == 0){
      System.out.println("Error: No input");
      return;
    }

    String filePath = args[0];
    String line = "";

    String sql = "insert into leka_products(product_id, product_name, product_price) values (?,?,?)";

    try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
        PreparedStatement prepStat = conn.prepareStatement(sql);
        BufferedReader inputReader = new BufferedReader(new FileReader(filePath))){

      Driver jdbcDriver = new oracle.jdbc.driver.OracleDriver();
      DriverManager.registerDriver(jdbcDriver);

      String[] products = null;

      while((line = inputReader.readLine()) != null) {
        products = line.split(",");

        int first = Integer.parseInt(products[0]);
        String second = products[1];
        Double third = Double.parseDouble(products[2]);

        prepStat.setInt(1, first);
        prepStat.setString(2, second);
        prepStat.setDouble(3, third);

        prepStat.executeUpdate();
      }

      System.out.println("Done!");

    } catch(Exception e) {
      e.printStackTrace();
    }

  }

实际上,此代码甚至还没有完成。它只是循环。

这种方法甚至是正确的吗?

0 个答案:

没有答案