Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

public class Main {

    public static void main(String args[]) throws Exception {
        Connection conn = null;
        Statement s = null;
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        conn = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};" + "Server=.\\SQLEXPRESS;"
                + "Trusted_Connection=yes;" + "Database=myDb");
        s = conn.createStatement();
        s.executeQuery("SELECT * FROM dbo.SalesSummary WHERE 0 = 1");
        ResultSet rs = s.getResultSet();
        ResultSetMetaData rsmd = rs.getMetaData();
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            System.out.println(String.format("-- Column %d --", i));
            System.out.println(String.format("Column name: %s", rsmd.getColumnName(i)));
            System.out.println(String.format("Database-specific type name: %s", rsmd.getColumnTypeName(i)));
            System.out.println(String.format("Column size (DisplaySize): %d", rsmd.getColumnDisplaySize(i)));
            System.out.println(String.format("java.sql.Type of column: %d", rsmd.getColumnType(i)));
            System.out.println();
        }
        s.close();
        conn.close();
    }
}