240 likes | 470 Views
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.
E N D
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 • 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
ODBC ODBC DB Server Client Front-end Application Started as a PC-standard. Now it’s an industry standard.
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
JDBC JDBC – ODBC Bridge DB Server Client Front-end Application Industry-wide portable standard
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
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
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
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>
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
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)); ”
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”);
More on ResultSets • Navigate to next row using: result.next(); • Extract specific data as in: String name = result.getString(“CDName”);
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();
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();
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
Sample Program using JDBC Simple JDBC application