JDBC
ODBC
- ODBC was invented by Microsoft to allow nework connection to databases
- Standardised and adopted by all other d/b vendors
- ODBC defines a C API for talking to SQL databases
- API calls allow submission of SQL queries and return of result sets
- API includes methods for stepping through result set
JDBC
- JDBC is Java interface to ODBC protocol
- JDBC drivers can be
- in C
- in Java
- use vendor specific protocol
- The easiest to install and use is a pure Java one
- A program using JDBC has to load the class files for the
particular JDBC implementation used
Example MySQL connection
This uses the MM JDBC driver for mySql
import java.sql.*;
public class Jdbc {
public static void main(String[] Args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
} catch (Exception E) {
System.err.println("Unable to load driver.");
E.printStackTrace();
}
try {
Connection myConn =
DriverManager.getConnection(
"jdbc:mysql://marigold.ise.canberra.edu.au/test");
} catch(Exception e) {
System.err.println("failed to connect");
e.printStackTrace();
}
Statement statement = connection.createStatement();
String sql = "INSERT INTO ...";
statement.executeQuery(sql);
statement.close();
}
}
Running mySql
- Run this once
/usr/local/mysql/scripts/mysql_install_db
- Start the server:
/usr/local/mysql/bin/safe_mysqld &
-
Shut the server down once they've finished doing stuff:
/usr/local/mysql/bin/mysqladmin -u root shutdown
Jan Newmarch (http://pandonia.canberra.edu.au)
jan@ise.canberra.edu.au
Last modified: Tue Oct 10 15:27:39 EST 2000
Copyright ©Jan Newmarch