Upload file maksudnya memasukkan data ke dalam database. disini saya akan mencoba memasukkan data dari file txt ke database.
Berikut simple code nya :
//tempat lokasi direktory txt
FileInputStream fstream = new FileInputStream("C:\\data.txt");
//tentukan objek di data input stream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
Class.forName("ini di isi dengan driver koneksi");
Connection con = DriverManager.getConnection("jdbc:odbc:nama database","user","pass");
//baca file
while ((strLine = br.readLine()) != null) {
// panggil isi file
String strar[] = strLine.split(";");
String query="";
try{
Statement st = con.createStatement();
jTextField.setText(strar[5]);
query = "INSERT into nm_db (a,aa,aaa,aaaa,aaaaa,aaaaa) VALUES('"+strar[0]+"','"+strar[1]+"','"+strar[2]+"','"+strar[3]+"','"+strar[4]+"','"+strar[5]+"')";
int val = st.executeUpdate(query);
jTextField.setText("Data Berhasil di upload");
}
catch (SQLException s){
jTextField.setText("SQL statement is not executed!");
}
}
in.close();
Upload data bisa juga dijadikan sebagai cara simple memasukkan data yang telah di backup dalam file txt,OK that's it..... just a simple word,
No comments:
Post a Comment