Advertisement
Guest User

Untitled

a guest
Jan 13th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. public class App
  2. {
  3. private static final String JDBC_DRIVER = "org.sqlite.JDBC";
  4. private static final String CONNECTION_STRING = "jdbc:sqlite:/tmp/testdb.db";
  5. public static void main( String[] args ) throws SQLException, ClassNotFoundException {
  6.  
  7. SQLiteDataSource ds = new SQLiteDataSource();
  8. ds.setUrl("jdbc:sqlite:/tmp/data.db");
  9. DBI dbi = new DBI(ds);
  10. Handle h = dbi.open();
  11. h.execute("create table something (id integer primary key, name text)");
  12.  
  13. h.execute("insert into something (id, name) values (?, ?)", 1, "Brian");
  14.  
  15. String name = h.createQuery("select name from something where id = :id")
  16. .bind("id", 1)
  17. .map(StringMapper.FIRST)
  18. .first();
  19.  
  20. System.out.println(name);
  21. h.close();
  22. }
  23.  
  24. private static Connection connection(String driver, String connectionString) throws SQLException, ClassNotFoundException {
  25. return connection(driver, connectionString, "", "");
  26. }
  27.  
  28. private static Connection connection(String driver, String connectionString, String username, String password) throws ClassNotFoundException, SQLException {
  29. Class.forName(driver);
  30. return DriverManager.getConnection(
  31. connectionString,
  32. username,
  33. password
  34. );
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement