Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Wang Bin 2004 JDBC ----Java Database Connectivity.

Similar presentations


Presentation on theme: "© Wang Bin 2004 JDBC ----Java Database Connectivity."— Presentation transcript:

1 © Wang Bin 2004 JDBC ----Java Database Connectivity

2 © Wang Bin 2004 In this lesson, you will learn to: u Identify the features of JDBC u Use JDBC to interact with a database Objectives

3 © Wang Bin 2004 u JDBC API:  Is a part of JDK  Enables Java applications to communicate with a database Getting Started

4 © Wang Bin 2004 3Contains a set of classes and interfaces that are used to connect to a database 3Is a low-level interface 3Can be used with both two-tier and three-tier database architectures Getting Started (Contd.)

5 © Wang Bin 2004 *JDBC Drivers: 3Are provided by a database vendor as a part of the database 3Enable applications to communicate with the database 3Are used by JDBC API to translate Java statements to SQL statements 3Make a Java application DBMS independent Getting Started (Contd.)

6 © Wang Bin 2004 *Categories of JDBC Drivers: 3JDBC-ODBC bridge driver ä Used with DBMS/RDBMSes that contain the ODBC driver embedded into them 3Native API partly Java driver ä Used with DBMS/RDBMSes that contain a JDBC driver supplied by the database vendor 3Native protocol pure Java driver/ JDBC-Net pure Java driver ä Used to connect a client application or applet to a database over a TCP/IP connection Getting Started (Contd.)

7 © Wang Bin 2004 *JDBC Driver Manager: 3Is used to maintain a list of drivers created for different databases 3Connects a Java application to the appropriate driver specified in a Java program *JDBC-ODBC Bridge: 3Is a driver provided by Sun Microsystems to access ODBC complaint databases from JDBC  Is implemented as the JdbcOdbc.class Getting Started (Contd.)

8 © Wang Bin 2004 *JDBC Application Architecture using the JDBC-ODBC bridge driver: Getting Started (Contd.)

9 © Wang Bin 2004 A person who wants to open an account with Bank has to submit his personal details by using the registration form available on the bank’s Website. The following program is used to accept the personal details of the customers. You need to modify the code to store the details in the Registration table of the Bank database. Use JDBC to Query a Database

10 © Wang Bin 2004 *Identify the generic steps involved in querying a database *Identify the mechanism to be used to load the driver *Identify the mechanism to be used to connect to a database *Identify the classes and the methods to be used to query a database *Create the DSN *Modify the program to store the customer registration details in the Bank database *Save, compile, and execute the program *Verify the data in the database Task List

11 © Wang Bin 2004 u The generic steps involved in querying a database are:  Loading the driver  Connecting to the database  Querying the database Task 1: Identify the generic steps involved in querying a database

12 © Wang Bin 2004 *Loading a Driver  Is done by using the forName() method of the Class class Result: 3The following statement will be used to load the JDBC- ODBC bridge driver and JDBC driver: Class.forName("sun.jdbc.odbc. JdbcOdbcDriver"); and Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDri ver" ); Task 2: Identify the mechanism to be used to load the driver

13 © Wang Bin 2004  The Connection Object 3Represents a connection with a database  The DriverManager. getConnection() method ä Is used to establish a connection with a database  Returns a Connection object ä Takes a JDBC URL as an input : : Task 3: Identify the mechanism to be used to connect to a database

14 © Wang Bin 2004  A dsn named “ MyDataSource ” has to be created  The following statement will be used to connect to the database: String url = "jdbc:odbc:MyDatasource"; Connection con = DriverManager.getConnection( url,”user1”,””); For JDBC you write as follow: Connection con = DriverManager.getConnection( "jdbc:microsoft:sqlserver://MyDbComputerNam eOrIP:1433;databaseName=master", sUsr, sPwd ); Task 3: Identify the mechanism to be used to connect to a database (Contd.)

15 © Wang Bin 2004 *The Statement Object: 3Is used to send simple queries to the database 3Contains the following methods:  executeQuery() ä executeUpdate() Task 4: Identify the classes and the methods to be used to query a database

16 © Wang Bin 2004 3Example Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection( "jdbc:odbc:MyDataSource",“user1",""); Statement stat=con.createStatement(); stat.executeQuery("Select * from Publishers"); Task 4: Identify the classes and the methods to be used to query a database (Contd.)

17 © Wang Bin 2004 *The ResultSet Object: 3Is generated on executing a statement 3Maintains a cursor pointing to the current row of data in the resultset 3Contains the following methods:  next()  getYYY() where YYY is the datatype Task 4: Identify the classes and the methods to be used to query a database (Contd.)

18 © Wang Bin 2004 3Example Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection( "jdbc:odbc:MyDataSource", "sa",""); Statement stat=con.createStatement(); ResultSet result=stat.executeQuery("Select * from Publishers"); while(result.next()) { System.out.println(result.getString(2)); } Task 4: Identify the classes and the methods to be used to query a database (Contd.)

19 © Wang Bin 2004 The administrative supervisor of a primary school needs to access the database to display the list of names of those teachers who joined the school after June 2000. Write the JDBC related code for: 3Loading the JDBC-ODBC bridge and establishing the connection 3The appropriate query statement (Hint: The table name is Teachers and the field name is TeacherName.) Just a Minute …

20 © Wang Bin 2004 *The PreparedStatement Object: 3Is used for executing parameterized queries  Is created by using the prepareStatement() method 3Example PreparedStatement stat=con.prepareStatement(“Select * from publishers where pub_id = ?”); Task 4: Identify the classes and the methods to be used to query a database (Contd.)

21 © Wang Bin 2004 3Parameters must be set before the statement is executed stat.setString(1,pid.getText()); ResultSet result=stat.executeQuery(); Result: 3 Class - forName() 3 DriverManager - getConnection() 3 Connection - prepareStatement() 3 PreparedStatement - executeUpdate() Task 4: Identify the classes and the methods to be used to query a database (Contd.)

22 © Wang Bin 2004 Task 5: Create the DSN Task 6: Modify the program to store the customer registration details in the Bank database Task 7: Save, compile, and execute the program

23 © Wang Bin 2004  Check whether the data has been inserted in the Registration table of the Bank database Task 8: Verify the data in the database

24 © Wang Bin 2004  The ResultSetMetaData interface:  Is used to obtain information about the columns stored in a ResultSet object  Contains the following methods: int getColumnCount() String getColumnName(int column_number) String getColumnTypeName(int column_number) Resultset Metadata

25 © Wang Bin 2004 In this lesson, you learned that: *JDBC API provides a database-programming interface for Java programs. A Java program can send queries to a database by using the JDBC driver  The java.sql package contains classes that help in connecting to a database, sending SQL statements to the database, and processing the query results  The Connection object represents a connection with a database. It can be initialized using the getConnection() method of the DriverManager class Summary

26 © Wang Bin 2004  The PreparedStatement object allows you to execute parameterized queries. It can be initialized using the prepareStatement() method of the Connection object  The setString () method sets the query parameters of the PreparedStatement object  The executeUpdate () method executes the query statement present in the PreparedSatement object and returns the number of rows affected by the query  The ResultSetMetaData interface is used to obtain information about the columns stored in a ResultSet object Summary (Contd.)


Download ppt "© Wang Bin 2004 JDBC ----Java Database Connectivity."

Similar presentations


Ads by Google