gpt4 book ai didi

java - 读取文本文件并将输出插入到mysql表中

转载 作者:行者123 更新时间:2023-11-29 11:41:16 26 4
gpt4 key购买 nike

我想读取txt文件内容并将其插入到我的sql表中。

我通过编码做到了这一点,但它显示了一些错误。也通过查询尝试过,但表中显示为空。

查询 -

INSERT INTO patent_uspto_tmp (pinv) VALUES (LOAD_FILE('/home/gaurav/Documents/pinv.txt'));

代码-

    try{
String arr[]=null;
String outputFile = "/home/gaurav/Documents/pinv.txt";

FileReader fileReader = new FileReader(outputFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String inputLine;
List<String> lineList = new ArrayList<String>();
while ((inputLine = bufferedReader.readLine()) != null) {
lineList.add(inputLine);
}
fileReader.close();

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/patent","root","india2000");
for (int i = 1;i<outputFile.length();i++)
{
arr[i]=outputFile.valueOf(i);

PreparedStatement ps = (PreparedStatement) con.prepareStatement("insert into patent_uspto_tmp (pinv) values(?)");
ps.setString(1,arr[i]); // set the param here with the some value
ps.executeUpdate();
con.commit();
con.close();
}
}
catch(Exception e)
{
throw new RuntimeException(e);//System.out.println(e);

}
}
}

error - null pointer exception

最佳答案

尝试使用 PreparedStatementexecuteUpdate() 以及最后的 commit() 方法,如下所示:

    PreparedStatement ps = con.prepareStatement("insert into patent_uspto_tmp (pinv) values(?)");
ps.setString(1, "value");// set the param here with the some value
ps.executeUpdate();
con.commit();

关于java - 读取文本文件并将输出插入到mysql表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35740260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com