. Java . Java JDBC

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); } } }
download source, use right-click and "Save Target As..." to save with a .java extension.

References

online

The Java 2 Platform Specification
Percy the Cat

Books

Thinking in Java by Bruce Eckel
Core Java (TM) 2 Volume 2 - Advanced Features by Cay Horstmann and Gary Cornell
Comments Comments are left by visitors to FluffyCat.com, are not endorsed by FluffyCat.com, and may or may not be accurate.
Comment by Larry Rate this Comment

in your class that needs the db:

DBConnection dbc = new DBConnection();
Connection connection = dbc.getConnection();


in a supporting class somewhere:

import java.sql.Connection;
import java.sql.DriverManager;

private Connection connection = null;

public DBConnection() {
//register the driver
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception ex) {
System.out.println("Exception getting driver");
System.out.println("Exception: " + ex);
}
}

public Connection getConnection() {
if (null == connection) {connect();}

return connection;
}

private void connect() {

try {

connection = DriverManager.getConnection("jdbc:mysql://localhost/YOURDBNAME?user=YOURUSERNAME&password=YOURPASSWORD");

} catch (java.sql.SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}

Comment by archive Rate this Comment

I downloaded java from sun and also MySQL 4.1

I want to write a program that can use MySQL under java. The examples are many, but there are problems with the driver. When I compile my program, it goes fine. However, I get exceptions when I run it complaining about the driver instruction.

MYSQL and JAVA work fine separately but not together.

Please let me know what I should do.

Sign In
to add your own comment