Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.

Similar presentations


Presentation on theme: "Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are."— Presentation transcript:

1 Java and database

2

3 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are fields The rows are records A field or set of fields that uniquely identifies a record is known as a primary key

4 4 eg Products & Customer

5 JDBC Introduction JDBC : Java database connectivity (JDBC) is the Java Soft specification of a standard application programming interface (API) that allows Java programs to access database management systems. The JDBC API consists of a set of interfaces and classes written in the Java programming language.

6 JDBC Introduction JDBC provides a standard library for accessing relational databases – API standardizes Way to establish connection to database Approach to initiating queries Method to create stored (parameterized) queries The data structure of query result (table) – Determining the number of columns – Looking up metadata, etc. – API does not standardize SQL syntax – JDBC is not embedded SQL – JDBC classes are in the java.sql package

7 JDBC classes Interface/class/exceptionDescription Interfaces: java.sql.Connection Interface used to establish a connection to a database. SQL statements run within the context of a connection. java.sql.DatabaseMetaData Interface used to return information about the database. java.sql.Driver Interface used to locate the driver for a particular database management system. java.sql.PreparedStatement Interface used to send precompiled SQL statements to the database server and obtain results. java.sql.ResultSet Interface used to process the results returned from executing an SQL statement. java.sql.ResultSetMetaData Interface used to return information about the columns in a ResultSet object. java.sql.Statement Interface used to send static SQL statements to the database server and obtain results. javax.sql.ConnectionEventListener Receives events that a PooledConnection object generates. javax.sql.ConnectionPoolDataSource Factory for PooledConnection objects. A ConnectionPoolDataSource object is usually registered with a JNDI service. javax.sql.DataSource A factory for Connection objects. A DataSource object is usually registered with a JNDI service provider. javax.sql.PooledConnection A PooledConnection object represents a physical connection to a data source. Classes: java.sql.Date Subclass of java.util.Date used for the SQL DATE data type. java.lang.DriverManager Class used to manage a set of JDBC drivers. java.sql.DriverPropertyInfo Class used to discover and supply properties to a connection. java.sql.Time Subclass of java.util.Date used for the SQL TIME data type. java.sql.TimeStamp Subclass of java.util.Date used for the SQL TIMESTAMP data type. java.sql.Types Class used to define constants that are used to identify standard SQL data types, such as CHAR, INTEGER, and DECIMAL. java.sql.String Class used to identify text data types such as CHAR. Exception classes: java.sql.SQLException Exception that provides information about a database error. java.sql.SQLWarning Exception that provides information about a database warning.

8 JDBC Drivers A JDBC driver provides database connection for a java program. Java uses the connection to interact with database. Before using a driver, the driver must be registered with the JDBC DriverManager.

9 JDBC Drivers 4 types of JDBC drivers Type 1 : JDBC-ODBC bridge It is used for local connection. ex) 32bit ODBC in windows Type 2 : Native API connection driver It is connected by the Native Module of dependent form of h/w like.dll or.so. ex) OCI driver for local connection to Oracle

10 JDBC Drivers Type 3 : Network connection driver Type 4 : Database Protocol driver It is independent from h/w because this driver is in Java. ex) thin driver for local/global connection to Oracle

11 Type 1: JDBC-ODBC Bridge Driver

12 Type 2 : Native API connection driver

13 Type 3 : Network connection driver

14 Type 4 : Database Protocol driver

15 Let consider the 2 types type 1 and type 4

16

17 Seven Basic Steps in Using JDBC 1. Load the driver 2. Define the Connection URL 3. Establish the Connection 4. Create a Statement object 5. Execute a query 6. Process the results 7. Close the connection

18 JDBC: Details of Process 1. Load the driver try { Class.forName("connect.microsoft.MicrosoftDriver"); Class.forName("oracle.jdbc.driver.OracleDriver"); } catch { ClassNotFoundException cnfe) { System.out.println("Error loading driver: " cnfe); }

19 JDBC: Details of Process 2. Define the Connection URL String host = "dbhost.yourcompany.com"; String dbName = "someName"; int port = 1234; String oracleURL = "jdbc:oracle:thin:@" + host + ":" + port + ":" + dbName; String sybaseURL = "jdbc:sybase:Tds:" + host + ":" + port + ":" + "?SERVICENAME=" + dbName;

20 JDBC: Details of Process(Continued) 3. Establish the Connection String username = "jay_debesee"; String password = "secret"; Connection connection = DriverManager.getConnection(oracleURL, username, password);

21 JDBC: Details of Process(Continued) 4. Create a Statement Statement statement = connection.createStatement(); 5. Execute a Query String query = "SELECT col1, col2, col3 FROM sometable"; ResultSet resultSet = statement.executeQuery(query); – To modify the database, use executeUpdate, supplying a string that uses UPDATE, INSERT, or DELETE – Use setQueryTimeout to specify a maximum delay to wait for results

22 JDBC: Details of Process (Continued) 6. Process the Result while(resultSet.next()) { System.out.println(resultSet.getString(1) + " " + resultSet.getString(2) + " " + resultSet.getString(3));} – First column has index 1, not 0 – ResultSet provides various getXxx methods that take a colu index or column name and returns the data – You can also access result meta data (column names, etc.) 7. Close the Connection connection.close(); – Since opening a connection is expensive, postpone this step if additional database operations are expected

23 HOW TO MAKE CONNECTION IN TYPE 1 AND 4

24 Type 1: JDBC-ODBC Bridge Drivers JDBC-ODBC driver uses a bridge technology to connect a java client to an ODBC database system. The JDBC-ODBC driver from SUN The driver is platform dependent and other non-java software need to be installed in the machine running the code.

25 Type 1: JDBC-ODBC Bridge Drivers Use sun.jdbc.odbc.JdbcOdbcDriver as the class name of the JDBC driver. – Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Use "jdbc:odbc:Northwind" as the database address, and use empty strings for the username and password. – Connection connection = DriverManager.getConnection "jdbc:odbc:my_database”,”user name”,”password”);

26 Type 4 : Database Protocol driver Load the vendor specific driver – Class.forName("oracle.jdbc.driver.OracleDriver"); Make the connection – Connection con = DriverManager.getConnection( "jdbc:oracle:thin:@oracle-prod:1521:OPROD", username, passwd);

27 After the connection is established

28 28 2. Create JDBC statement(s) Statement stmt = con.createStatement() ; Creates a Statement object for sending SQL statements to the database

29 29 Executing SQL Statements String createLehigh = " Create table Lehigh " + " (SSN Integer not null, Name VARCHAR(32), " + " Marks Integer) " ; stmt.executeUpdate(createLehigh); //What does this statement do? String insertLehigh = " Insert into Lehigh values “ + " (123456789,abc,100) " ; stmt.executeUpdate(insertLehigh);

30 30 Get ResultSet String queryLehigh = " select * from Lehigh " ; ResultSet rs = Stmt.executeQuery(queryLehigh); //What does this statement do? while (rs.next()) { int ssn = rs.getInt( " SSN " ); String name = rs.getString( " NAME " ); int marks = rs.getInt( " MARKS " ); }

31 31 Close connection stmt.close(); con.close();

32


Download ppt "Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are."

Similar presentations


Ads by Google