I have tried the following code to split the csv values and now how do insert it in to DB? Do I have save the values in to separate variables to match column names? I am unable to figure out.
Note: I don't want to use any csv parser right now. I just want to do it manually
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
String name;
String email;
String phone;
String ID;
Connection con = OracleDBConnection.getConnection();
String query = "Insert into NEWSTUDENT values(?,?,?,?)";
PreparedStatement st = con.prepareStatement();
st.executeUpdate(query);
try {
BufferedReader bReader = new BufferedReader(new FileReader("1000rows.csv"));
while (bReader != null) {
String read;
try {
read = bReader.readLine();
if (read != null)
{
String[] array = read.split(",+");
for(String result:array)
{
System.out.println(result);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
finally
{
if (bReader == null)
{
bReader.close();
}
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}
output:
1Kiriti
[email protected]
880789939
Column names in Database:
Name Email Phone ID