Documente Academic
Documente Profesional
Documente Cultură
JDBC
JDBC is a Java Database Connectivity API that lets you access virtually any tabular data source from a Java application. JDBC defines a low-level API designed to support basic SQL functionality independently of any specific SQL implementation. The main strength of JDBC is that it is designed to work in exactly the same way with any relational database. In other words, it isn't necessary to write one program to access an Oracle database, another to access a Sybase database, another for SQL Server, and so on. The three main functions of JDBC are as follows: Establishing a connection with a database or other tabular data source Sending SQL commands to the database Processing the results
Get a connection to the database. Create a statement. Execute a SQL query. Retrieve data from the ResultSet.
import java.sql.*; // imports the JDBC core package public class JdbcDemo{ public static void main(String args[]){ int qty; float cost; String lastName, firstName; // SQL Query string String query = "SELECT LastName,FirstName FROM Employees"; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // load the JDBC driver Connection con = DriverManager.getConnection ("jdbc:odbc:MyDataBase"); // get a connection Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); // execute query
ODBC
Short for Open DataBase Connectivity, a standard database access method developed by the SQL Access group in 1992. The goal of ODBC is to make it possible to access any data from any application, regardless of which database management system (DBMS) is handling the data. ODBC manages this by inserting a middle layer, called a database driver , between an application and the DBMS. The purpose of this layer is to translate the application's data queries into commands that the DBMS understands. For this to work, both the application and the DBMS must be ODBCcompliant -- that is, the application must be capable of issuing ODBC commands and the DBMS must be capable of responding to them.
Java application
method call
functions call
JDBC-ODBC bridge +
DBMSs Microsoft Access Driver (*.mdb) Microsoft FoxPro Driver (*.dbf) Microsoft Excel Driver (*.xls) Relational or non-relational data sources (databases, spreadsheets, XML files)
Prepared Statement
import java.sql.*; public class PreparedStmt{ public static void main(String args[]){ String name, title, city; String[] cities={"London","Seattle"}; String query = ("SELECT * FROM Employees WHERE City = ?"); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Names and titles of Connection con = employees from the DriverManager.getConnection ("jdbc:odbc:MyDataBase"); PreparedStatement pstmt = con.prepareStatement(query); table Employees, for (int i=0; i<2;i++){ grouped on city pstmt.setString(1, cities[i]); ResultSet rs = pstmt.executeQuery(); System.out.println(cities[i]); while (rs.next()) { name = rs.getString("FirstName")+" "+rs.getString("LastName"); title = rs.getString("Title"); System.out.println(name+"\t"+title); } } } catch(ClassNotFoundException e){ e.printStackTrace(); } catch(SQLException e){ e.printStackTrace(); } } }
rs.updateString("City", "London"); rs.updateRow(); for(int i=1;i<=nColumns;i++){ System.out.print(rs.getString(i)+((i==nColumns)?"\n":"\t")); } } } catch(ClassNotFoundException e){ e.printStackTrace(); } catch(SQLException e){ e.printStackTrace(); } } }
Bibliography
John ODonahue: Java Database Programming Bible, Wiley, 2002