-
-
Save asad-awadia/f5a8393fb35c807307bdbee6ee3c6184 to your computer and use it in GitHub Desktop.
SQLite + JDBI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class App | |
{ | |
private static final String JDBC_DRIVER = "org.sqlite.JDBC"; | |
private static final String CONNECTION_STRING = "jdbc:sqlite:/tmp/testdb.db"; | |
public static void main( String[] args ) throws SQLException, ClassNotFoundException { | |
SQLiteDataSource ds = new SQLiteDataSource(); | |
ds.setUrl("jdbc:sqlite:/tmp/data.db"); | |
DBI dbi = new DBI(ds); | |
Handle h = dbi.open(); | |
h.execute("create table something (id integer primary key, name text)"); | |
h.execute("insert into something (id, name) values (?, ?)", 1, "Brian"); | |
String name = h.createQuery("select name from something where id = :id") | |
.bind("id", 1) | |
.map(StringMapper.FIRST) | |
.first(); | |
System.out.println(name); | |
h.close(); | |
} | |
private static Connection connection(String driver, String connectionString) throws SQLException, ClassNotFoundException { | |
return connection(driver, connectionString, "", ""); | |
} | |
private static Connection connection(String driver, String connectionString, String username, String password) throws ClassNotFoundException, SQLException { | |
Class.forName(driver); | |
return DriverManager.getConnection( | |
connectionString, | |
username, | |
password | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>io.github.davepkennedy</groupId> | |
<artifactId>jbdi-sqlite</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>jbdi-sqlite</name> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.11</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.jdbi</groupId> | |
<artifactId>jdbi</artifactId> | |
<version>2.55</version> | |
</dependency> | |
<dependency> | |
<groupId>org.xerial</groupId> | |
<artifactId>sqlite-jdbc</artifactId> | |
<version>3.7.15-M1</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment