import java.sql.*;
import java.io.*;
     
public class Try  {

	public static void main(String args[]) {
		  
		String url = "jdbc:odbc:";
		Connection con;
		// THIS IS WHERE YOU TYPE IN YOUR FAVORITE QUERY
		String query = "select * from sysobjects";

		Statement stmt;	

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		String line,user = "",passwd = "";
		try {
		    // THIS IS WHERE YOU ENTER YOUR ODBC DSN NAME e.g. sample
		    System.out.print("Enter the name of the data source (e.g. sample): ");
		    line = in.readLine();
		    url += line;
    		    // THIS IS WHERE YOU ENTER YOUR USERNAME e.g. user35
		    System.out.print("Enter user name (e.g. user35): ");
		    user = in.readLine();
		    System.out.print("Enter the password: ");
		    passwd = in.readLine();
		}
		catch (IOException e)
		{
		    System.err.println(e);
		}


		try {
     		      // IN THIS CLASS, WE WILL USE SUN'S JDBC-ODBC BRIDGE, THAT IS INSTALLED ON THE LAB MACHINES
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		} catch(java.lang.ClassNotFoundException e) {
			System.err.print("ClassNotFoundException: ");
			System.err.println(e.getMessage());
		}
	
		try {
			// THIS IS THE CONNECTION STRING. FOR EXAMPLE, IF YOU WERE CONNECTING TO ORACLE,
			// YOU WOULD POINT TO AN ORACLE ODBC DSN IN THE URL, AND USE THE ODBC DRIVER FOR 
			// FOR ORACLE TO CONNECT TO AN ORACLE DATABASE, WITH THE ASSOCIATED USER AND
			// PASSWORD.
			con = DriverManager.getConnection(url, user, passwd);
			System.out.println("Connection established");
			System.out.println("Executing query: "+query);

			stmt = con.createStatement();
			printResults(stmt.executeQuery(query));
			stmt.close();
			
			System.out.println("Closing connection");
			con.close();

		} catch(SQLException ex) {
			System.err.print("SQLException: ");
			System.err.println(ex.getMessage());
		}	
	}
    static void printResults(ResultSet rs) throws SQLException
    {
	ResultSetMetaData rsmd = rs.getMetaData();
	int numberOfColumns = rsmd.getColumnCount();
	int rowCount = 1;
	while (rs.next()) {
	    System.out.println("Row " + rowCount + ":  ");
	    for (int i = 1; i <= numberOfColumns; i++) {
		System.out.print("   Column " + i + ":  ");
		System.out.println(rs.getString(i));
	    }
	    System.out.println("");
	    rowCount++;
	}	
    }
}

