sqlite-jdbc
sqlite-jdbc copied to clipboard
Add this example of a more modern demo Sample.java
Add this example of a more modern demo Sample.java
import java.sql.*;
public class SampleModern {
public static void main(String[] args) {
try (Connection connection = DriverManager.getConnection("jdbc:sqlite:target/demo-modern.db");
Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY)
) {
connection.setAutoCommit(false);
statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("drop table if exists person");
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'leo')");
statement.executeUpdate("insert into person values(2, 'yui')");
ResultSet rs = statement.executeQuery("select * from person");
while( rs.next() ) {
System.out.println("name = " + rs.getString("name"));
System.out.println("id = " + rs.getInt("id"));
}
connection.commit();
rs.close();
} catch(SQLException e) {
System.err.println(e.getMessage());
}
}
}
sqlite-jdbc is compiled with an older version of Java, so you are better off by publishing this sample as a GitHub Gist.
Personally I'd like to use this modern example. I think we should create an example code folder inside the code base.
One comment:
- ResultSet still needs to be exception safe (try block should be used as well)
@xerial - I like the idea of using the modern example too, but if it is in the code base, it should compile. Otherwise, it could get stale. Should we create a new Maven build for sample code, using the latest JDK version?
Creating 'example' folder and building it with Maven is a good idea. It can work as an integration test.
Sent from my Sony F5321 using FastHub
Is this still relevant in 2022?