Download presentation
Presentation is loading. Please wait.
1
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
Embedded SQL Prof: Dr. Shu-Ching Chen TA: Sheng Guan
2
Problems with using interactive SQL
Standard SQL is not “Turing-complete”. • E.g., Two profs are “colleagues” if they’ve co-taught a course. • We can’t write a query to find all colleagues of a given professor because we have no loops or recursion. • You can’t control the format of its output. • And most users shouldn’t be writing SQL queries! • You want to run queries that are based on user input, not have users writing actual queries.
3
SQL + a conventional language
• If we can combine SQL with code in a conventional language, we can solve these problems. • But we have another problem: • SQL is based on relations, and conventional languages have no such type. • It is solved by • feeding tuples from SQL to the other language one at a time, and • feeding each attribute value into a particular variable.
4
JDBC
5
JDBC - Simple Example Steps:
Load the driver and register it with the driver manager Connect to a database Create a statement Execute a query and retrieve the results, or make changes to the database Disconnect from the database
6
JDBC - Simple Example API for accessing and processing DB data
import java.sql.*; public class jdbctest { public static void main(String args[]){ try{ Class.forName("org.postgresql.Driver"); Connection con = DriverManager.getConnection ("jdbc:postgresql://hugo.cs.fiu.edu:5432/testdb", "user", "pass"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery ("select name, number from mytable where number < 2"); while( rs.next() ) System.out.println(rs.getString(1) + " (" + rs.getInt(2) + ")"); rs.close(); stmt.close() con.close(); } catch(Exception e){ System.err.println(e); } } } Java launcher looks for “main” method “catch” an Exception here (could be an SQLException)
7
JDBC Example.java: Import SQL Package, Prepare DB Objects
Drafting out the sample input forms, queries and reports, often helps.
8
JDBC Example.java: Load the driver!
Drafting out the sample input forms, queries and reports, often helps.
9
JDBC Example.java: Make the connection to the DB..
Drafting out the sample input forms, queries and reports, often helps.
10
JDBCExample.java: Create + Execute a Statement!
Drafting out the sample input forms, queries and reports, often helps.
11
What do I need to run this code ?
Download the driver Compile the code javac JDBCExample.java Drafting out the sample input forms, queries and reports, often helps.
12
JDBCExample.java: Import SQL Package, Prepare DB Objects
Drafting out the sample input forms, queries and reports, often helps.
13
JDBCExample.java: Load the driver!
Drafting out the sample input forms, queries and reports, often helps.
14
JDBCExample.java: Make the connection to the DB..
Drafting out the sample input forms, queries and reports, often helps.
15
JDBCExample.java: Create + Execute a Statement!
Drafting out the sample input forms, queries and reports, often helps.
16
JDBCExample.java: INSERTs, UPDATEs..
Drafting out the sample input forms, queries and reports, often helps.
17
JDBCExample.java: Use Prepared Statement for Insertion!
18
JDBCExample.java: Use Prepared Statement for Insertion!
Result of using Prepared Statement for Insertion :
19
What is “preparation” ? Preparing a statement includes parsing the SQL, compiling and optimizing it. The resulting PreparedStatement can be executed any number of times without having to repeat these steps.
20
If the query isn’t known until run time
• You can hard-code in the parts you know, and use “?” as a placeholder for the values you don’t know. • This is enough to allow a PreparedStatement to be constructed. • Once you know values for the placeholders, methods setString, setInt, etc. let you fill in those values. Drafting out the sample input forms, queries and reports, often helps.
21
JDBCExample.java: Get and Process a Result Set..
Drafting out the sample input forms, queries and reports, often helps.
22
JDBCExample.java: DROP a table and close connection..
Drafting out the sample input forms, queries and reports, often helps.
23
Reference s/embedded/Embedded.pdf dedSQL/ Drafting out the sample input forms, queries and reports, often helps.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.