Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
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,myURL CHAR);");
        st.executeUpdate("create view surveyview as (select * from survey);");
        getTables(conn);

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

    public static void getTables(Connection conn) throws Exception {
        String TABLE_NAME = "TABLE_NAME";
        String TABLE_SCHEMA = "TABLE_SCHEM";
        String[] TABLE_AND_VIEW_TYPES = { "TABLE", "VIEW" };
        DatabaseMetaData dbmd = conn.getMetaData();

        ResultSet tables = dbmd.getTables(null, null, null, TABLE_AND_VIEW_TYPES);
        while (tables.next()) {
            System.out.println(tables.getString(TABLE_NAME));
            System.out.println(tables.getString(TABLE_SCHEMA));
        }
    }

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

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