sqlite-jdbc icon indicating copy to clipboard operation
sqlite-jdbc copied to clipboard

Add this example of a more modern demo Sample.java

Open djangofan opened this issue 8 years ago • 5 comments

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());
    }
  }
}

djangofan avatar Jul 04 '17 18:07 djangofan

sqlite-jdbc is compiled with an older version of Java, so you are better off by publishing this sample as a GitHub Gist.

schemacrawler avatar Aug 15 '17 03:08 schemacrawler

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 avatar Aug 15 '17 05:08 xerial

@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?

schemacrawler avatar Aug 15 '17 20:08 schemacrawler

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

xerial avatar Aug 16 '17 16:08 xerial

Is this still relevant in 2022?

gotson avatar Jul 29 '22 05:07 gotson