* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download CS348 – INFORMATION SYSTEMS
Concurrency control wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Microsoft Access wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Oracle Database wikipedia , lookup
Clusterpoint wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Relational model wikipedia , lookup
Database model wikipedia , lookup
Versant Object Database wikipedia , lookup
CS348 – INFORMATION SYSTEMS Romila Pradhan Introduction to JDBC What is JDBC? Java DataBase Connectivity - Java API to access RDBMS - Facilitates Java applications to: - connect to a data source, e.g., a database - send queries and update statements to the database - retrieve and process the results Architecture Java Application JDBC API provides access to relational data from Java JDBC Driver Manager JDBC Driver defines objects that connect Java applications to a JDBC driver Oracle Steps to process SQL statements 1. 2. 3. 4. 5. Establish a connection Create a statement Execute the query Process the ResultSet object Close the connection Sample Program Download from https://www.cs.purdue.edu/homes/rpradhan/cs348/s17/musicDB.java 1. Establish a connection Connect to Oracle through a Connection object Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@claros.cs.purdue.edu:1524:strep", ”USERNAME",”PASSWORD"); USERNAME: OracleAccount@csora PASSWORD: OraclePassword Compile and Run Source the driver ($ORACLE_HOME/ojdbc6.jar) Compile: javac -cp $ORACLE_HOME/ojdbc6.jar musicDB.java Run java -cp .:$ORACLE_HOME/ojdbc6.jar musicDB 2. Create a statement - An interface that represents a SQL statement. - executing Statement objects, - generate ResultSet objects - Three different kinds of statements: 1. Statement: To implement SQL statements with no parameters 2. PreparedStatement: For precompiling SQL statements that might contain input parameters 3. CallableStatement: To execute stored procedures that may contain both input and output parameters 3. Execute the query Three kinds of execute methods from Statement: 1. executeQuery - if the query retrieves some data from the database returns one ResultSet object 2. executeUpdate - for SQL statements that update/modify the database 3. execute (can be used for any kind of SQL statement) - returns “true” if the SQL query was a SELECT returns “false” if it was an UPDATE, INSERT or DELETE 4. Process the results - Query results in a ResultSet object - Access the data in a ResultSet object through a cursor - points to one row of data in the ResultSet object NOT a database cursor initially, the cursor is positioned before the first row methods defined in the ResultSet object move the cursor 5. Close the connection When you are finished using a Statement, call the method Statement.close() to immediately release the resources it is using - the corresponding ResultSet objects are also closed Recap: Steps to process SQL statements 1. 2. 3. 4. 5. Establish a connection Create a statement Execute the query Process the ResultSet object Close the connection