import java.sql.*; import java.io.*; public class db_connect { public static void main (String[] args) { try { // Loading the JDBC-ODBC Driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Establishing a connection // NOTE: You have to put in your database name, as well as your given username and password. Connection con = DriverManager.getConnection("jdbc:odbc:your-db-name","your-username", "your-password"); // Statement objects are used for executing a static SQL statement and // obtaining the results produced by it. Statement stmt = con.createStatement(); // Now let's assume that you have created and populated a table called MyTable with // two attributes - MyField1 of type string and MyField2 of type int. // (Look at the tutorials given on the project writeup page.) // Here is how to query the database (using a simple SELECT statement) from a program // in Java. // The result of a query needs to be assigned to a ResultSet instance (as below). ResultSet rs = stmt.executeQuery("SELECT MyField1, MyField2 FROM MyTable"); // Now, the variable rs contains the resulting (selected) rows of the table. // The method next() moves the "cursor" to the next row of the result and makes // that row available to operate on. // NOTE: To read even the first row of the result, you will have to call next(). while (rs.next()) { String s = rs.getString("MyField1"); int n = rs.getInt("MyField2"); // Methods are of the form: getString(), getFloat(), getInt(), etc. // Their arguments can be either the name of the column to be retrieved, // or its index in the result - the very first column being with index 1. // Print out a row of data values (ordered by columns) System.out.println(s + " " + n); } // Close the instance of ResultSet. rs.close(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } } }