· 10 years ago · Sep 07, 2016, 03:16 PM
1package so39359521;
2
3import java.sql.*;
4
5public class So39359521Main {
6
7 public static void main(String[] args) {
8 String connStr = "jdbc:sqlite:/home/gord/__tmp/mydatabase.sqlite";
9 try {
10 Class.forName("org.sqlite.JDBC");
11 try (Connection connection = DriverManager.getConnection(connStr)) {
12 final String TABLE = "test_table";
13 //The statement used to build the table I'm testing on
14 final String BUILD_TABLE_STATEMENT =
15 "CREATE TABLE IF NOT EXISTS " + TABLE + " (id varchar(32) NOT NULL UNIQUE, test BLOB NOT NULL, PRIMARY KEY (id));";
16 try (Statement st = connection.createStatement()) {
17 st.executeUpdate(BUILD_TABLE_STATEMENT);
18 st.executeUpdate("DELETE FROM " + TABLE);
19 }
20 byte[] myBytes = new byte[] { 0x36, 0x24, 0x36 };
21 String sql = "INSERT INTO " + TABLE + " (id, test) VALUES (?,?)";
22 try (PreparedStatement ps = connection.prepareStatement(sql)) {
23 ps.setString(1, "foo");
24 ps.setBytes(2, myBytes);
25 ps.executeUpdate();
26 }
27 }
28 } catch (Exception e) {
29 e.printStackTrace(System.err);
30 }
31 }
32
33}