Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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 CLOB);");

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

        File file = new File("c:/Java_Dev/data.txt");

        FileReader reader = new FileReader(file);
        pstmt.setCharacterStream(1, reader);

        pstmt.execute();

        ResultSet resultSet = pstmt.executeQuery("select b from survey ");

        File data = new File("C:\\a.txt");
        Reader dataReader = resultSet.getCharacterStream(1);
        FileWriter writer = new FileWriter(data);
        char[] buffer = new char[1];
        while (dataReader.read(buffer) > 0) {
            writer.write(buffer);
        }
        writer.close();

        reader.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", "");
    }
}