200 likes | 272 Views
JAVA and working with. Databases. Download: http://homel.vsb.cz/ ~ciz034/ cze/study.html. Helena Pomezná, ciz034 St. skupina: L392. FEI, VŠB-TUO Ak. rok. 2002/2003. Some facts about JDBC How to make our application. What is the JDBC API?. JDBC = Java Database Connectivity
E N D
JAVA and working with Databases Download: http://homel.vsb.cz/~ciz034/cze/study.html Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003
Some facts about JDBC • How to make our application
What is the JDBC API? • JDBC = Java Database Connectivity • a Java API for accessing virtually any kind of tabular data • Consists of a set of classes and interfaces that provide a standard API for tool/database developers • Makes it easy to send SQL statements to relational database systems • Supports all dialects of SQL.
The value of JDBC • Can access virtually any data source • Can run on any platform with a JVM • We don’t have to worry about writing different applications to run on different platforms =>lets a programmer write once and run anywhere.
What does the JDBC API do? A JDBC technology-based driver = JDBC driver makes it possible to do 3 things: • Establish a connection with a data source • Send queries and update statements to the data source • Process the results.
The JDBC versus ODBC ODBC API (Open DataBase Connectivity) was the most widely used programming interface for accessing databases.
Why not use ODBC from Java? • You can use ODBC from Java, but this is best done with the help of the JDBC API in the form of the JDBC-ODBC Bridge. • ODBC is not appropriate for direct use from the Java because it uses a C interface. • A literal translation of the ODBC C API into a Java API would not be desirable. • ODBC is hard to learn.
Java Software Framework Java Software provides 3 JDBC product components: • the JDBC driver manager (included in Java2 Platform) • the JDBC driver test suite (on JDBC web site) • the JDBC-ODBC bridge (included in the Solaris and Windows versions of the Java2 Platform)
How to do it? • Installation • Setting Up a Database • Establishing a Connection • Creating Complete JDBC Applications
Installation • Install Java and JDBC on your machine • You will get JDBC when you download the JDK • Install driver on your machine • JDBC driver • JDBC-ODBC Bridge driver • ODBC driver
Setting Up a Database We will assume that the database already exists. When you create the tables as examples below, they will be in the default database.
Establishing a Connection Loading Drivers • We want to use the JDBC-ODBC Bridge Class.forName( “sun.jdbc.odbc.JdbcOdbcDriver”); • For instance, if the class name is jdbc.DriverXYZ Class.forName(“jdbc.DriverXYZ”);
Establishing a Connection 2 Making the connection Connection con = DriverManager.getConnection (url, “myLogin”, “myPassword”);
Creating complete JDBC applications • Putting code in class definition • Importing classes to make them visible import java.sql.*; • Using try and catch blocks SQLException ClassNotFoundException • Using SQL
Using SQL Creating a Table String createTableContact = “CREATE TABLE CONTACT“ + “(FIRST VARCHAR(32), LAST VARCHAR(32), EMAIL VARCHAR(32))”; Creating JDBC Statement Statement stmt = con.createStatement(); stmt.executeUpdate(createTableContact);
Using SQL - continuation Entering data into a table first line: stmt.executeUpdate(“INSERT INTO CONTACT “ + “VALUES (‘Thomas’, ‘Willson’, ‘t.wil@vili.com’)”); second line: stmt.executeUpdate(“INSERT INTO CONTACT “ + “VALUES (‘Jane’, ‘Willson’, ‘j.wil@vili.com’)”);
Simple example import java.sql.*; Public class Lookup{ public static void main(String[] args) throws SQLException, ClassNotFoundException{ String dbUrl = “jdbc:odbc:contact”; String user = “”; String pass = “”; Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Connection con = DriverManager.getConnection( dbUrl, user, pass); Statement sta = con.createStatement();
Simple example - continuation ResultSet res = sta.executeQuery( “SELECT FIRST, LAST, EMAIL “ + “FROM contact.csv contact” + “WHERE” + “(LAST = ‘ “ + args[0] + “,) “ + “ORDER BY FIRST”); while(res.next()){ System.out.println(res.getString(“Last”) + “, “ + res.getString(fiRST”) + “: “ + res.getString(“EMAIL”)); } sta.close(); }
Sources • http://java.sun.com/docs/books/tutorial - the Java Tutorial • Thinking in Java 2nd Edition, Bruce Eckel