1 / 19

Java Database Connectivity (JDBC)

Java Database Connectivity (JDBC). ODBC JDBC SQL Using Java as a front-end. SQL. SQL was standardized in 1992 To use SQL commands: Connect to a database server Issue valid SQL commands Collect results back from the database server Problem : Each vendor has a different interface. ODBC.

dobry
Download Presentation

Java Database Connectivity (JDBC)

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 Database Connectivity (JDBC) ODBC JDBC SQL Using Java as a front-end

  2. SQL • SQL was standardized in 1992 • To use SQL commands: • Connect to a database server • Issue valid SQL commands • Collect results back from the database server • Problem: Each vendor has a different interface

  3. ODBC • Open DataBase Connectivity • C-based interface to SQL-based database engine • Provides a consistent interface for communicating with a database and for accessing database metadata

  4. ODBC ODBC DB Server Client Front-end Application Started as a PC-standard. Now it’s an industry standard.

  5. JDBC • Java DataBase Connectivity • Java offers portability • JDBC allows developers to develop front-end applications for a variety of platforms with the portability power of Java

  6. JDBC JDBC – ODBC Bridge DB Server Client Front-end Application Industry-wide portable standard

  7. JDBC-ODBC Bridge • A layer between the Java front-end application and the database server • Client talks to JDBC • JDBC communicates with underlying ODBC • ODBC communicates with database server • Results passed in reverse order to above

  8. Necessary classes • Connection: Establishes a connection with the SQL database driver manager • Statement: Allows developer to frame fixed SQL queries • PreparedStatement: Allows developer to frame SQL queries with parameters • ResultSet: Used to collect the results of an SQL query

  9. Step 1: Load the JDBC-ODBC Bridge • Load using: Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); • Alternatively, run the application from the command line as: java –Djdbc.drivers = sun.jdbc.odbc.JdbcOdbcDriver application.java

  10. Step 2: Instantiate a Connection object • Set up a connection between the client and the JDBC-ODBC bridge, using the DriverManager • Connection con = DriverManager.getConnection(URL, username, password); • URL = jdbc:<subprotocol>:<database URL>

  11. Step 3: Instantiate a Statement Object • Instantiate a Statement object to frame SQL queries and execute them • Statement stmt = con.CreateStatement(); • execute(): used for CREATE operations • executeQuery(): used for SELECT queries • executeUpdate(): used for UPDATE queries

  12. Step 4: Create tables using execute() • E.g. • stmt.execute(“create table CD_MASTER ” + “(CD_NO number ” + “CONSTRAINT pk_cd PRIMARY KEY,” + “ CD_NAME varchar2(25),” + “ ARTIST varchar2(25), ” + “ TYPE varchar2(15)); ”

  13. Step 5: Use ResultSet to collect results • Execute queries using stmt.executeQuery() and collect them in a ResultSet object, as: • ResultSet result = stmt.executeQuery(“select * from CD_MASTER”);

  14. More on ResultSets • Navigate to next row using: result.next(); • Extract specific data as in: String name = result.getString(“CDName”);

  15. Step 6: PreparedStatement • Extension of the Statement object • Used when performing the same operation multiple times • E.g. PreparedStatement ps = con.prepareStatement(“INSERT INTO CD_MASTER VALUES (?, ?, ?, ?)”); • Set specific positional values later: • E.g. ps.setInt(1, 101); ps.setString(2, “Fields of Gold”); • Execute using ps.executeUpdate();

  16. Step 7: Stored Procedures • Use stmt.execute() to create a stored procedure • Create a CallableStatement object per stored procedure as: • CallableStatement cs = con.prepareCall(“{call <proc_name>(arg1, arg2, …)}”); • Set specific positional values • Execute using: cs.executeUpdate();

  17. Step 8: Trap exceptions • SQLException: • Thrown whenever a critical error is encountered • Possible causes: wrong SQL query, problems with database driver, etc. • SQLWarning: • Not thrown, but can be trapped whenever a non-critical error is encountered using the getWarnings() method of the Connection object

  18. Step 9: Maintaining Java-SQL Type Conformance

  19. Sample Program using JDBC Simple JDBC application

More Related