Java program to insert data to database from a file
import java.io.*;
import java.sql.*;
import java.util.*;
public class TestFile_to_DB {
public static void main(String[] args) throws Exception {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/jdbc","root","root");
Statement s = con.createStatement();
FileInputStream fin = new FileInputStream("G://test.txt");
//you should manully enter data to you file
// LIKE SO:
//java,lab
//ds,lab
Scanner sc = new Scanner(fin);
while(sc.hasNext()){
StringTokenizer st = new StringTokenizer(sc.nextLine(),",");
//here you use any delimiter of your choice
while (st.hasMoreTokens()) {
s.execute("insert into testdb values(' "+st.nextToken()+" ',' "+st.nextToken()+" ');");
//here my table consistes of two string fields
}
}
}
//after the program terminates open your table to see the output
}
import java.sql.*;
import java.util.*;
public class TestFile_to_DB {
public static void main(String[] args) throws Exception {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/jdbc","root","root");
Statement s = con.createStatement();
FileInputStream fin = new FileInputStream("G://test.txt");
//you should manully enter data to you file
// LIKE SO:
//java,lab
//ds,lab
Scanner sc = new Scanner(fin);
while(sc.hasNext()){
StringTokenizer st = new StringTokenizer(sc.nextLine(),",");
//here you use any delimiter of your choice
while (st.hasMoreTokens()) {
s.execute("insert into testdb values(' "+st.nextToken()+" ',' "+st.nextToken()+" ');");
//here my table consistes of two string fields
}
}
}
//after the program terminates open your table to see the output
}
Comments
Post a Comment