我想讀取txt文件內容並將其插入到我的sql表中。讀取文本文件並將輸出插入到mysql表中
我通過編碼做到了這一點,但它顯示出一些錯誤。也嘗試通過查詢,但它在表中顯示爲空。
查詢 -
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
請告訴我的錯誤? – TheLostMind
運行查詢後,它在mysql表中顯示爲空。如果我運行的代碼,我越來越像這樣 - 用戶連接爲 –
prerna