mysql - how to read the content of the file and store in database using java -
i need read file column , need store in database. problem file contents not stored in database , code read contents. getting error in storing it.
code:
public class test3 { private static string vrms; private static string irms; private static string total; public static connection getconnection() { seek { 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"); homecoming conn; } grab (exception e) { system.out.println("connection not established"); homecoming 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; seek { conn = getconnection(); conn.setautocommit(false); file file = new file(strline); fis = new fileinputstream(file); pstmt = conn.preparestatement("insert meter1(vrms, irms, total) values (?, ?, ?)"); pstmt.setstring(1, vrms); pstmt.setstring(2, irms); pstmt.setstring(3, total); pstmt.executeupdate(); conn.commit(); } grab (exception e) { system.err.println("error: " + e.getmessage()); e.printstacktrace(); } { pstmt.close(); fis.close(); conn.close(); }}}
error: 10 11 0 12 13 0 14 15 0 connection established error: null java.lang.nullpointerexception @ java.io.file.(unknown source) @ vidhya.test3.main(test3.java:71) exception in thread "main" java.lang.nullpointerexception @ vidhya.test3.main(test3.java:85)
this sample code might helpful.. replace column names right 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); } } grab (ioexception e) {system.out.println("there 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 allpatients(name, age, height, weight) values(?,?,?,?)"); preparedstatement.setstring(1, name); preparedstatement.setstring(2, age); preparedstatement.setstring(3, height); preparedstatement.setstring(4, weight); preparedstatement.executeupdate(); }
java mysql
No comments:
Post a Comment