Skip to content

Instantly share code, notes, and snippets.

@davepkennedy
Created July 1, 2014 11:26
Show Gist options
  • Save davepkennedy/724345eed1d4e550bc9b to your computer and use it in GitHub Desktop.
Save davepkennedy/724345eed1d4e550bc9b to your computer and use it in GitHub Desktop.
SQLite + JDBI
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
);
}
}
<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>
@vadimov
Copy link

vadimov commented Nov 6, 2015

package my.sqlite3.jdbi;

import java.sql.SQLException;

import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.util.StringMapper;

/**
 * SQLite + JDBI
 * java -cp "./target/sqlite3-jdbi-0.0.1-SNAPSHOT.jar" my.sqlite3.jdbi.App
 */
public class App {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        DBI dbi = new DBI("jdbc:sqlite:data.sqlite");
        Handle h = dbi.open();

        h.execute("DROP TABLE IF EXISTS something");
        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();
    }
}
<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>my.sqlite3.jdbi</groupId>
  <artifactId>sqlite3-jdbi</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>sqlite3-jdbi</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>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.jdbi</groupId>
        <artifactId>jdbi</artifactId>
        <version>2.62</version>
    </dependency>
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.8.11.2</version>
    </dependency>
  </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>     
</project>

@asad-awadia
Copy link

Thanks
very helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment