Java JDBC
GetTheCatsFavoriteFood.java
import java.sql.*;
class GetTheCatsFavoriteFood { public static void main(String[] args) { try { Class.forName("oracle.jdbc.driver.OracleDriver"); //loads the driver class //an alternative is to use: // DriverManager.registerDriver( // new oracle.jdbc.driver.OracleDriver()); // which creates a new instance of the // Oracle driver and registers it Connection connection = DriverManager.getConnection( "jdbc:oracle:thin:@fluffycat:1521:catinfo", "percy", "sardine"); //connects to the Oracle catinfo database at fluffycat, // logs in as "percy" with a password of "sardine" Statement statement = connection.createStatement(); //this is what we use to tie our connection to our SQL ResultSet resultSet = statement.executeQuery( "Select catname, favoritefood from favoritefoods"); //now run the SQL while (resultSet.next()) { System.out.println(resultSet.getString(1) + "'s favorite food is " + resultSet.getString(2)); } //traverse the ResultSet, //The cat names are in the first column, and the // favorite foods are in the second column. //note: the subscript of the first column is one, // not zero. resultSet.close(); statement.close(); connection.close(); } catch(java.lang.ClassNotFoundException cnfException) { System.out.println( "The following error occured " + "in finding the Oracle driver: " + cnfException); } catch(SQLException sqlException) { System.out.println( "The following error occured in " + "reading from the favoritefoods table: " + sqlException); } } }
References
| Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate. |
| Comment by Larry on 2006-07-03 Rate this Comment |
in your class that needs the db:
|
| Comment by archive on 2006-07-03 Rate this Comment |
I downloaded java from sun and also MySQL 4.1
|