Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class Main {
    public static void main(String[] args) throws Exception {
        Connection conn = getConnection();
        Statement st = conn.createStatement();

        st.executeUpdate("create table survey (Id int, b BLOB);");

        PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)");

        File file = new File("blob.txt");
        FileInputStream fis = new FileInputStream(file);
        pstmt.setBinaryStream(1, fis, (int) file.length());
        pstmt.execute();

        fis.close();
        st.close();
        conn.close();
    }

    private static Connection getConnection() throws Exception {
        Class.forName("org.hsqldb.jdbcDriver");
        String url = "jdbc:hsqldb:mem:data/tutorial";

        return DriverManager.getConnection(url, "sa", "");
    }
}