简体   繁体   English

如何使用Java读取文件的内容并存储在数据库中

[英]how to read the content of the file and store in database using java

I need to read a file by column and I need to store it in a database. 我需要按列读取文件,并将其存储在数据库中。 My problem is the file contents are not stored in the database and the code just read the contents. 我的问题是文件内容未存储在数据库中,而代码仅读取了内容。 I am getting an error in storing it. 我在存储它时遇到错误。

Code: 码:

public class Test3 {

private static String vrms;
private static String irms;
private static String total;

public static Connection getConnection() {
try {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test3";
String username = "root";
String password = "";

Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username,password);
System.out.println("Connection Established");
return conn;
} catch (Exception e) {
System.out.println("Connection not established");
return null;
}   }
public static void main(String[] args) throws Exception {
FileInputStream fstream = new FileInputStream("D:/data/database.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null)  
{
strLine.split(" ");
System.out.println(strLine);
} 
in.close();
FileInputStream fis = null;
PreparedStatement pstmt = null;
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
File file = new File(strLine);
fis = new FileInputStream(file);
pstmt = conn.prepareStatement("insert into meter1(vrms, irms, total) values (?, ?, ?)");
pstmt.setString(1, vrms);
pstmt.setString(2, irms);
pstmt.setString(3, total);
pstmt.executeUpdate();
conn.commit();
} 
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
} finally {
pstmt.close();
fis.close();
conn.close();
}}}

error: 10 11 0 12 13 0 14 15 0 Connection Established Error: null java.lang.NullPointerException at java.io.File.(Unknown Source) at vidhya.Test3.main(Test3.java:71) Exception in thread "main" java.lang.NullPointerException at vidhya.Test3.main(Test3.java:85) 错误:10 11 0 12 13 0 14 15 0建立的连接错误:null在vidhya.Test3.main(Test3.java:71)处java.io.File。(未知源)处的java.lang.NullPointerException线程“ main”中的异常“ vidhya.Test3.main(Test3.java:85)处的java.lang.NullPointerException

You have not stored information read from the file in any variable (vrms, irms or total). 您尚未以任何变量(vrms,irms或total)存储从文件读取的信息。 Correct your code before posting here for the issue. 在发布此问题之前,请更正您的代码。

Posting the sample data file and column data types will be helpful. 发布样本数据文件和列数据类型将很有帮助。

This is sample code which might be helpful.. Replace the column names with correct db columns. 这是示例代码,可能会有所帮助。用正确的db列替换列名。

public static void main(String[] args) throws IOException, SQLException {

PreparedStatement preparedstatement = null;


try{
    String read=null;
    in = new BufferedReader(new FileReader("patientlist.txt")); 
    while ((read = in.readLine()) != null) {
        String[] splited = read.split("\\s+");
        name=splited[0];
        age=splited[1];
        height=splited[2];
        weight=splited[3];      
        addpatient(connection, preparedstatement, name, age, height, weight);
    }

}
catch (IOException e) {System.out.println("There was a problem: " + e);}
    if (connection != null)
    try{connection.close();} catch(SQLException ignore){} 
}


public static void addpatient(Connection connection, PreparedStatement preparedstatement, String name, String age, String height, String weight) throws SQLException{
preparedstatement=connection.prepareStatement("insert into allpatients(name, age, height, weight) values(?,?,?,?)");
preparedstatement.setString(1, name);
preparedstatement.setString(2, age);
preparedstatement.setString(3, height);
preparedstatement.setString(4, weight);
preparedstatement.executeUpdate();


}

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

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