package sqlitetest; import java.sql.*; public class SqliteTest { public static void main(String args[]) { Connection connection = null; try { /* The next line will give a ClassNotFound error unless the sqlite jar is in the classpath. To add it in Netbeans, right-click on the project, choose Properties, Libraries and add the path to the jar file. On Linux I saved it in /usr/local/sqlite/sqlite-jdbc-3.20.0.jar, but this will be different if you are using Mac or Windows. */ Class.forName("org.sqlite.JDBC"); connection = DriverManager.getConnection("jdbc:sqlite:test.db"); // The database must already have a country table with these fields. // In my table there is also a CountryId primary key field, but the // database assigns values to that field automatically. String sql = "insert into Country (CountryName, Population) values (?, ?)"; PreparedStatement ps = connection.prepareStatement(sql); // In larger applications you would probably loop through a list // of country objects. This is just a simple demo. ps.setString(1, "United States of America"); ps.setString(2, "320000000"); ps.addBatch(); ps.setString(1, "China"); ps.setString(2, "1400000000"); ps.addBatch(); ps.executeBatch(); connection.close(); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); System.exit(0); } System.out.println("Insert successful"); } }