public static void main(String[] args) {
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:C:\\users\\tim\\airline\\flightschedule.db");
PreparedStatement statement = connection.prepareStatement("INSERT INTO flights (flightID,departure,arrival)VALUES(?,?,?)");
statement.setInt(1,5);
statement.setString(2,"David");
statement.setString(3,"Ortiz");
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Asked
Active
Viewed 1.0k times
0

tim
- 970
- 4
- 12
- 20
-
Can you paste the exception trace you're seeing? – lobster1234 Apr 25 '11 at 02:40
2 Answers
10
You should call a different method.
First things first though:
Bad code (wide open to SQL Injection attack):
statement = connection.createStatement();
resultSet = statement.executeQuery(
"INSERT INTO flights
('flightID','departure','arrival')
VALUES('"+flightID+"','"+departure+"','"+arrival+"')");
Good code:
PreparedStatement statement = connection.prepareStatement(
"INSERT INTO flights (flightID,departure,arrival)
VALUES(?,?,?)");
statement.setString(1,flightID);
statement.setString(2,departure);
statement.setString(3,arrival);
statement.executeUpdate();
// thanks to @lobster1234 for reminder!
connection.commit();
Have you noticed I do executeUpdate() instead of executeQuery()? Because this is the cause of your trouble.
P.S. I also noticed that you pass flightID into the method as int, but insert it into database as a string. Not a good practice usually. Stick to one datatype. If ID is really a number, make it a number in the database and then call setInt(1,flightID); alternatively, pass it around as String too.

Vladimir Dyuzhev
- 18,130
- 10
- 48
- 62
-
I do not see any exceptions, but the data is not being added into the database. – tim Apr 25 '11 at 02:53
-
This is because the query only scans the DB. Frankly, I would expect SQLite to throw an exception on passing INSERT into query method, but I guess being lite means you can cut corners. – Vladimir Dyuzhev Apr 25 '11 at 02:58
-
Thanks, I changed it to executeUpdate() and used the prepared statements. It's still not successfully adding it to the db though. – tim Apr 25 '11 at 03:03
-
OK, please update the code in the question to the one you have now. Please provide the table structure. Also, executeUpdate() returns an int -- number of updated/inserted records. Dump that one to console. – Vladimir Dyuzhev Apr 25 '11 at 03:06
-
I opened a new class (minus the method) to test out the code and am now getting nullpointerexceptions at the 3 .close() lines. When I took them all out it worked fine and the data was inserted successfully... but I don't want to leave the connection open. – tim Apr 25 '11 at 03:20
-
1You get NPE because you do not use resultSet. Remove it at all. And add commit() as @lobster1234 suggested. Living in the world of declarative transaction demarcation, I forgot one need to do one :D – Vladimir Dyuzhev Apr 25 '11 at 03:28
4
Try calling connection.commit()
after executeUpdate()
. You can also get the value returned by executeUpdate()
and make sure you get 1 and not 0, as this call returns the number of rows affected by the statement.

lobster1234
- 7,679
- 26
- 30
-
hehe... now I wonder if the original version with passing INSERT into query() would work too with SQLite after commit() added... – Vladimir Dyuzhev Apr 25 '11 at 03:30
-
I hear ya! I've been spoilt by Spring-JDBC where you dont have to deal with the boilerplate code so much :) – lobster1234 Apr 25 '11 at 03:33