import java.sql.*; // Import JDBC package public class Employee { public static void main (String args []) throws Exception { String url = "jdbc:mysql://holland.mathcs.emory.edu:3306/"; String dbName = "companyDB"; String userName = "cs377"; String password = "abc123"; /* ================================== Load the MySQL JDBC driver ================================== */ DriverManager.registerDriver( new com.mysql.jdbc.Driver() ); /* ===================================== Connect to the MySQL database server ===================================== */ Connection conn = null; conn = DriverManager.getConnection(url+dbName,userName,password); /* ============================================== Create a Statement object to process queries ============================================== */ Statement stmt = conn.createStatement (); VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV Ready to process SQL query You can submit multiple queries with the stmt variable ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ /* -------------------------------- Submit a query for execution -------------------------------- */ ResultSet rset = stmt.executeQuery ("select * from employee"); /* ---------------------------------------------------------- Iterate through the result and process tuples rset.next( ) returns FALSE when there are no tuples left ---------------------------------------------------------- */ while ( rset.next () ) { System.out.println (rset.getString (1) + " " // Get column (attr) 1 in tuple + rset.getString (2) + " " // Get column (attr) 2 in tuple + rset.getString (3) + " " // and so on + rset.getString (4) + " " + rset.getString (5) + " " + rset.getString (6) + " " + rset.getString (7) + " " + rset.getString (8) + " " // We assume there are at least 8 attrs !! ); } /* ----- Close the ResultSet ----- */ rset.close(); VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV Make sure you FREE the rset set when you are done !!! Statements between these 2 banners can be in a loop.... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ /* ----- Close the Statement ----- */ stmt.close(); /* ----- Close the connection ----- */ conn.close(); } } |
|