Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων Εξάμηνο 7 ο
JDBC JDBC is a set of classes and interfaces written in Java that allows Java programs to send SQL statements to a database like Oracle
JDBC drivers There are two categories of Oracle JDBC drivers: OCI and JDBC Thin. Oracle's JDBC Thin driver is based on Java and is platform independent you need to download the ojdbc6.jar file and save it on your system. Oracle's OCI driver uses Oracle's native client libraries to communicate with databases. These libraries are obtained as part of the Oracle Instant Client. A good example of the OCI driver use is accessing a remote Oracle database from a PHP application using the Oracle Instant Client libraries.
JDBC database Connector The JDBC database Connector provides access to the database. To reach the database using JDBC we need a JDBC driver from the database provider in our case Oracle. This connector is typically delivered with the product in a jar or zip file or available in the provider s website. These files must be in our classpath
Connect Oracle με Java Βήμα 1 - Εισάγουμε το πακέτο JAVA : import java.sql.*; Βήμα 2 Register JDBC Driver Η εγγραφή του driver είναι η διαδικασία με την οποία το Oracle class driver s class φορτώνεται στη μνήμη έτσι ώστε να μπορεί να χρησιμοποηθεί Βήμα 3 - Δημιουργούμε τη σύνδεση με τη ΒΔ: Connection conn= DriverManager.getConnection( main protocol:sub protocol:type4driver:name of oracle server:default port number(1521):service name, username, password ); Αρα jdbc:oracle:thin:@[host][:port]:sid π.χ. Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:orcl", HR", hr"
JDBC Database Connections Import JDBC packages: import java.sql.* Register Driver Class.forName("oracle.jdbc.driver.OracleDriver"); DriverManager.registerDriver( new oracle.jdbc.oracledriver()); Create Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/orcl","hr", "hr"); Close Connection con.close();
Creating Statements A Statement is an interface that represents a SQL statement. You execute Statement objects, and they generate ResultSet objects, which is a table of data representing a database result set. You need a Connection object to create a Statement object. create a Statement object with the following code: stmt = con.createstatement();
Kinds of Statements Statement: Used to implement simple SQL statements with no parameters. PreparedStatement: (Extends Statement.) Used for precompiling SQL statements that might contain input parameters. See Using Prepared Statements for more information. CallableStatement: (Extends PreparedStatement.) Used to execute stored procedures that may contain both input and output parameters.
Executing Queries To execute a query, call an execute method from Statement such as the following: execute: Returns true if the first object that the query returns is a ResultSet object. Use this method if the query could return one or more ResultSet objects. Retrieve the ResultSet objects returned from the query by repeatedly calling Statement.getResultSet. executequery: Returns one ResultSet object. executeupdate: Returns an integer representing the number of rows affected by the SQL statement. Use this method if you are using INSERT, DELETE, or UPDATE SQL statements.
ResultSet (2)
Execute SQL query String SQL = "SELECT CUSTNO, CUSTNAME FROM CUSTOMER"; Statement stat = conn.createstatement(); ResultSet rs = stat.executequery(sql); while (rs.next()) { System.out.println( rs.getint(1) + "\t" + rs.getstring(2) ); } stat.close(); conn.close();
Matching Java and SQL Data Types
Executing SQL statements : PreparedStatement
ResultSet PreparedStatement.executeUpdate only returns the number of affected records PreparedStatement.executeQuery returns data, encapsulated in a ResultSet object (a cursor) ResultSet rs=pstmt.executequery(sql); // rs is now a cursor While (rs.next()) { // process the data }
add the Java Oracle Connector JAR in project s classpath.to do this, right click on your Java Project -> Properties -> Buildpath -> Libraries -> Add External JAR and select the odbc6.jar file.
Call a procedure with an IN parameter { CallableStatement procin = conn.preparecall ("begin procin (?); end;"); procin.setstring (1, "testing"); procin.execute (); procin.close(); }
Call a function with an IN parameter } { CallableStatement funcin = conn.preparecall ("begin? := funcin (?); end;"); funcin.registeroutparameter (1, Types.CHAR); funcin.setstring (2, "testing"); funcin.execute (); System.out.println ("Return value is: " + funcin.getstring (1)); funcin.close();