Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming: JDBC Vyacheslav Grebenyuk CTDE, AI dept., KhNURE.

Similar presentations


Presentation on theme: "Java Programming: JDBC Vyacheslav Grebenyuk CTDE, AI dept., KhNURE."— Presentation transcript:

1 Java Programming: JDBC Vyacheslav Grebenyuk CTDE, AI dept., KhNURE

2 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20062 Content

3 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20063 9/30/2016 JDBC JDBC (Java Database Connectivity) API allows Java programs to connect to databases Database access is the same for all database vendors The JVM uses a JDBC driver to translate generalized JDBC calls into vendor specific database calls There are four general types of JDBC drivers  We will look at Type 4 …

4 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20064 9/30/2016 Pure Java Driver (Type 4) These drivers convert the JDBC API calls to direct network calls using vendor-specific networking protocols by making direct socket connections with the database It is the most efficient method to access database, both in performance and development time It is the simplest to deploy All major database vendors provide pure Java JDBC drivers for their databases and they are also available from third party vendors For a list of JDBC drivers, refer to  http://industry.java.sun.com/products/jdbc/drivers

5 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20065 9/30/2016 Java Application DB Client Pure Java Driver (2) JDBC API JDBC Driver Data Source Server

6 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20066 9/30/2016 Typical JDBC Programming Procedure 1. Load the database driver 2. Obtain a connection 3. Create and execute statements (SQL queries) 4. Use result sets (tables) to navigate through the results 5. Close the connection

7 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20067 9/30/2016 Driver Manager The purpose of the java.sql.DriverManger class in JDBC is to provide a common access layer on top of different database drivers used in an application DriverManager requires that each driver required by the application must be registered before use, so that the DriverManager is aware of it Load the database driver using ClassLoader :  Class.forName (“oracle.jdbc.driver.OracleDriver”);

8 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20068 9/30/2016 Connecting to a Database Type 4 JDBC Driver – Oracle Server Class.forName (“oracle.jdbc.driver.OracleDriver”); con = DriverManager.getConnection ( “jdbc:oracle:thin:@bonsai.ite.gmu.edu:1521:ite”, “accountname", “password”); Type 4 JDBC Driver – MySQL Server Class.forName (“org.gjt.mm.mysql.Driver”); con = DriverManager.getConnection (“jdbc:mysql://localhost/databasename”, uid, passwd);

9 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20069 9/30/2016 Creating Tables Creating a Coffee table CREATE TABLE COFFEES (COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, SALES INTEGER, TOTAL INTEGER) Creating JDBC statements Statement stmt = con.createStatement (); Execute a statement stmt.executeUpdate (“CREATE TABLE COFFEES “ + “(COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, “ + “SALES INTEGER, TOTAL INTEGER)”); SQL query

10 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200610 9/30/2016 Execute Statements This uses executeUpdate because the SQL statement contained in createTableCoffees is a DDL (data definition language) statement Statements that create a table, alter a table, or drop a table are all examples of DDL statements and are executed with the method executeUpdate executeUpdate is also used to execute SQL statements that update a table

11 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200611 9/30/2016 Execute Statements In practice, executeUpdate is used far more often to update tables than it is to create them because a table is created once but may be updated many times The method used most often for executing SQL statements is executeQuery executeQuery is used to execute SELECT statements, which comprise the vast majority of SQL statements

12 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200612 9/30/2016 Entering Data into a Table Statement stmt = con.createStatement(); stmt.executeUpdate ( "INSERT INTO COFFEES " + "VALUES ('Colombian', 101, 7.99, 0, 0)" ); stmt.executeUpdate ( "INSERT INTO COFFEES " + "VALUES ('French_Roast', 49, 8.99, 0, 0)" ); stmt.executeUpdate ( "INSERT INTO COFFEES " + "VALUES ('Espresso', 150, 9.99, 0, 0)" ); stmt.executeUpdate ( "INSERT INTO COFFEES " + "VALUES ('Colombian_Decaf', 101, 8.99, 0, 0)" ); stmt.executeUpdate ( "INSERT INTO COFFEES " + "VALUES ('French_Roast_Decaf', 49, 9.99, 0, 0)" );

13 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200613 9/30/2016 Getting Data From a Table ResultSet rs = stmt.executeQuery ("SELECT COF_NAME, PRICE FROM COFFEES"); while (rs.next()) { String s = rs.getString ("COF_NAME"); float n = rs.getFloat ("PRICE"); System.out.println (s + " " + n); }

14 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200614 9/30/2016 JNDI Connection Manager JDBC Data Source Architecture Application JDBC Database

15 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200615 Instead of conclusion


Download ppt "Java Programming: JDBC Vyacheslav Grebenyuk CTDE, AI dept., KhNURE."

Similar presentations


Ads by Google