简体   繁体   中英

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)

You have not stored information read from the file in any variable (vrms, irms or 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.

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();


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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