i´m writing my first program and trying to transfer data with an ArrayList (from a CSV database) to an SQLite database using JDBC Driver sqlite-jdbc-3.7.2.jar. I have already read tutorials für SQLite and java beginners and also different answers to questions on stackoverflow (Link 1,Link 2 ) and i can finally create a db with tables and variable columns (datatype: TEXT). But when i try to add record from an ArrayList to my testtable with this method:
/**
* Add record from ArrayList<string> to table
* @param selectTable (name of table)
* @param line (List with record for table)
*/
public void addRecord (String selectTable, ArrayList<String> line){
try{
//connect to database
connection = new DBconnect(pathDataBase);
database = connection.getConnection();
//create SQL Statement
ArrayList<String> columnNames = new ArrayList<String>();
columnNames = getColumnList(selectTable); //call method to get all columnNames from selected table
String sSQL = "INSERT INTO " + selectTable + "("; //update statement (string sSQL)
//disperse ArrayList<string> columnNames in parts to add to the statement(string sSQL)
int i = 0;
for (i=0; i<columnNames.size(); i++){
sSQL = sSQL + columnNames.get(i);
if (columnNames.size() + 1 > i+2){
sSQL = sSQL + ", "; //update statement(string sSQL)
}//end if
}// end for
sSQL = sSQL + ") VALUES("; //update statement(string sSQL)
//disperse ArrayList<string> line in parts to add to the statement(string sSQL)
i=0;
for (i=0; i<columnNames.size(); i++){
sSQL = sSQL + line.get(i); //add record per line in columns
if (columnNames.size() + 1 > i+2){
sSQL = sSQL + ", ";
}//end if
}//end for
sSQL = sSQL + ");";
System.out.println(sSQL);
Statement statement = database.createStatement();
System.out.println("created statement");
statement.executeUpdate(sSQL);
System.out.println("executed Update");
statement.close();
database.close();
//catch exception
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}//end try/catch
}//end method
i get the following exception description:
Opened database successfully
INSERT INTO Testtable(Test1, Test2, Test3) VALUES(Hallo1, Hallo2, Hallo3); //(ArrayList<string> columnNames) (ArrayList<string> line)
created statement
java.lang.NullPointerException: null
I think something in my String "sSQL" has to be wrong.
Does someone know what i have to change in order to make it work? Can i not write the datatype "string" in a "Text" column?
I hope i described my problem understandable and asked the right questions.
Many thanks in advance. :)
@all: Oh man, i stumbled over my own stupidity. I opened a connection to database and call a method, which opens also a connection and closes it. of course now i can´t execute a statement, because my database is closed. Thanks Naren for your time!!!