1 / 27

Introduction to JDBC

Introduction to JDBC. Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu. Ways to Access DB. PL/SQL. Direct SQL. External language connected to DB. What is JDBC. JDBC: Java Database Connectivity JDBC is a standard interface for connecting to relational databases from Java. Java code.

liluye
Télécharger la présentation

Introduction to 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. Introduction to JDBC Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu

  2. Ways to Access DB PL/SQL Direct SQL External language connected to DB

  3. What is JDBC • JDBC: Java Database Connectivity • JDBC is a standard interface for connecting to relational databases from Java Java code DB

  4. Overview of Querying a Database With JDBC

  5. Stage 1: Connect

  6. JDBC Driver Java app JDBCdriver Database JDBCdriver Database JDBC calls Database commands JDBCdriver Database Inside the code, you make calls using JDBC APIs Software module that translates JDBC calls to SQL commands

  7. JDBC Driver • Is an interpreter that translates JDBC method calls to vendor-specific database commands • Implements interfaces in java.sql • Can also provide a vendor’s extensions to the JDBC standard

  8. How to Make the Connection 1. Register the driver. Class.forName(“oracle.jdbc.driver.OracleDriver”); Or DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());

  9. How to Make the Connection 2. Connect to the DB Connection conn = DriverManager.getConnection (URL, userid, password); In our Oracle DB Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@oracle.wpi.edu:1521:WPI11grxx", "userid", "password"); Your Oracle username and pw

  10. Import java.sql package Register the driver Establish connection

  11. Stage 2: Query the DB

  12. JDBC Statement Object • A Statement object sends your SQL command to the database • You need an active connection to create a JDBC statement • Statement has methods to execute a SQL statement: • executeQuery() for QUERY statements • executeUpdate() for INSERT, UPDATE, DELETE

  13. How to Query the Database The query string Number of affected tuples Output relation

  14. Querying the Database: Example I

  15. Querying the Database: Example II Execute a select statement Statement stmt = conn.createStatement(); String str ="SELECT * FROM users”; Resultset rset = stmt.executeQuery(str); Build your SQL command in a separate string and then pass it for execution

  16. Stage 3: Process Results

  17. Resultset Object • JDBC returns the results of a query in a ResultSetobject. • A ResultSetmaintains a cursor pointing to its current row of data. • Use next() to step through the result set row by row. • getString(), getInt(), and so on assign each value to a Java variable.

  18. How to Process Results

  19. Example Statement stmt = conn.createStatement(); String str ="SELECT branch_name, amount FROM R”; Resultset rset = stmt.executeQuery(str); While ( rset.next()) { String bName = rset.getString(“branch_name”); int amt = rset.getInt(“amount”); … System.out.println(”Name:” + bName + ” Amount: ” + amt); }

  20. Getxxx Function over Resultset And many more: http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

  21. Stage 4: Closing

  22. How to Close

  23. JDBC PrepareStatement • If execute the statement multiple times • Use a PrepareStatementobject • It is compiled once and used multiple times • PrepareStatement may contain variables • Placeholder for actual values supplied at execution time

  24. How to create PrepareStatement “?” Is the placeholder for variables

  25. How to Execute For SQL queries For Insert/Update/ Delete With each execution set the values and then execute…

  26. How to Connect to WPI Oracle 1- Log in to CCC machine 2- Set environment variables > source /usr/local/bin/oraenv 3- Set CLASSPATH for java > export CLASSPATH=./:/usr/local/oracle11gr203/product/11.2.0/ db_1/jdbc/lib/ojdbc6.jar 4- Write your java code (say file name is OracleTest.java) and then compile it > JavacOracleTest.java 5- Run it > Java OracleTest

  27. Sources Some links with examples http://www.cs.ubc.ca/~ramesh/cpsc304/tutorial/JDBC/jdbc1.html http://infolab.stanford.edu/~ullman/fcdb/oracle/or-jdbc.html

More Related