20-CS-694 Advanced Programming Techniques Spring 2012
JDBC

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous      Next      PostgreSQL Docs      MySQL Docs      PostgreSQL      MySQL      All lectures

SQL Statement

import java.sql.*;
     
public class SQLStatement {
   public static void main(String args[]) {
      String url = "jdbc:mySubprotocol:myDataSource";
      Connection con;
      String query = "select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME " +
                  "from COFFEES, SUPPLIERS " +
                  "where SUPPLIERS.SUP_NAME like 'Acme, Inc.' and " +
                  "SUPPLIERS.SUP_ID = COFFEES.SUP_ID";
      Statement stmt;
   
      try {
         Class.forName("myDriver.ClassName");
      }
      catch(java.lang.ClassNotFoundException e) {
         System.err.print("ClassNotFoundException: ");
         System.err.println(e.getMessage());
      }
   
      try {
         con = DriverManager.getConnection(url, "myLogin", "myPassword");
   
         stmt = con.createStatement();                     
   
         ResultSet rs = stmt.executeQuery(query);
         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++;
         }
         stmt.close();
         con.close();

      }
      catch(SQLException ex) {
         System.err.print("SQLException: ");
         System.err.println(ex.getMessage());
      }   
   }
}