1 / 20

Databases

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

Download Presentation

Databases

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Some facts about JDBC • How to make our application

  3. 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.

  4. 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.

  5. 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.

  6. The JDBC versus ODBC ODBC API (Open DataBase Connectivity) was the most widely used programming interface for accessing databases.

  7. 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.

  8. 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)

  9. How to do it? • Installation • Setting Up a Database • Establishing a Connection • Creating Complete JDBC Applications

  10. 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

  11. 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.

  12. 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”);

  13. Establishing a Connection 2 Making the connection Connection con = DriverManager.getConnection (url, “myLogin”, “myPassword”);

  14. 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

  15. 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);

  16. 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’)”);

  17. 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();

  18. 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(); }

  19. Sources • http://java.sun.com/docs/books/tutorial - the Java Tutorial • Thinking in Java 2nd Edition, Bruce Eckel

  20. Thank you

More Related