Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
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 con = DriverManager.getConnection("jdbc:h2:mem:");
        Statement s = con.createStatement();
        s.execute("CREATE TABLE Table1 (Column1 CLOB)");

        InputStream is = new FileInputStream("data.txt");
        Reader rdr = new InputStreamReader(is, StandardCharsets.ISO_8859_1);
        PreparedStatement ps = con.prepareStatement("INSERT INTO Table1 (Column1) VALUES (?)");
        ps.setCharacterStream(1, rdr);
        ps.executeUpdate();

        ResultSet rs = s.executeQuery("SELECT Column1 FROM Table1");
        int rowNumber = 0;
        while (rs.next()) {
            String str = rs.getString("Column1");
            System.out.println(String.format("Row %d: CLOB is %d character(s) long.", ++rowNumber, str.length()));
        }
        rs.close();
        con.close();
    }
}