Download presentation
Presentation is loading. Please wait.
Published byKathlyn Day Modified over 8 years ago
1
JDBC I IS 313 1.21.2003
2
Why do we have databases?
3
Why not just use files?
4
Four perspectives on the database Large data store Persistent data store Query service Transaction service
5
Programmer’s view Not how it works how to administer it how to design a database Database services persistent storage sophisticated querying transactions
6
JDBC Group of Java classes Correspond to basic database concepts Connecting to the database Issuing a query Examining results
7
Higher-level option Enterprise JavaBeans simply state that an object is going to be persistent the EJB “container” uses JDBC to save the object in a database SE 554
8
Database table 1103PaulMcCartney99987 1500JohnLennon99996 1101AaronPresley34345 0124ElvisPresley32324 175FredFlintstone20203 053SuzyQ56782 175RobinBurke12341 Fuel Option DaysCar type First name Last nameIDRow #
9
Key relationships 175FredFlintstone2020 053SuzyQ5678 175RobinBurke1234 Fuel Option DaysCar type First name Last nameID SUV5 Luxury4 Full size3 Mid-size2 Compact1 Subcompact0 DescriptionID
10
Basic terms Table basic unit of organization Row / Record single “entry” Column named attributed of a row View a dynamically created table Query an operation on the database typically retrieval of a view Record set / result set / row set the information returned by a query
11
SQL Declarative language not procedural Describe what you want database figures out “how” In general do as much in SQL as you can
12
Query SELECT LastName, FirstName FROM Reservations
13
Sorting SELECT * FROM Reservations ORDER BY Reservations.LastName;
14
Choosing SELECT * FROM Reservations WHERE (LastName = ‘Presley’) AND (CarType = 1)
15
SQL Language Case insensitive Strings enclosed in single quotes Whitespace ignored Commands for (creating and structuring databases) retrieving data inserting rows deleting rows Different versions SQL-92 most widely supported
16
SELECT statement SELECT { columns } FROM { table(s) } WHERE { criteria }... other options... ;
17
Whole table SELECT * FROM Reservations
18
Simple criteria SELECT * FROM Reservations WHERE (LastName = ‘Presley’) AND (CarType = 1)
19
Complex criteria SELECT titles.type, titles.title FROM titles t1 WHERE titles.price > (SELECT AVG(t2.price) FROM titles t2 WHERE t1.type = t2.type)
20
Note Dot syntax for columns table.column titles.type Table aliases table alias titles t1
21
Join SELECT Reservations.ID, Reservations.LastName, Reservations.FirstName, CarType.Description, Reservation.Days, FuelOption.Description FROM Reservations, CarType, FuelOption WHERE Reservations.CarType = CarType.ID AND Reservations.FuelOption = FuelOption.ID
22
Join example 175RobinBurke1234 Fuel Option DaysCar type First name Last nameID Pre-paid fuel1 DescriptionID SUV5 DescriptionID Pre-paid fuel7SUVRobinBurke1234 F.DescriptionR.DaysC.DescriptionR.FirstNameR.LastNameR.ID
23
Join example cont’d What if? SUV5 DescriptionID Sport Ut.5
24
JDBC
25
DriverManager knows how to connect to differents DBs Connection represents a connection to a particular DB Statement a query or other request made to a DB ResultSet results returned from a query
26
Loading the driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); What is this?
27
Creating a connection Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = “jdbc:odbc:mydatabase”; Connection conn = DriverManager.getConnection(url, ”UserName", ”Password"); Why not “ new Connection () ”?
28
Property file jdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver jdbc.url=jdbc:odbc:rentalcars jdbc.username=PUBLIC jdbc.password=PUBLIC
29
Using properties public static Connection getConnection() throws SQLException, IOException { Properties props = new Properties(); String fileName = “rentalcars.properties"; FileInputStream in = new FileInputStream(fileName); props.load(in); String drivers = props.getProperty("jdbc.drivers"); if (drivers != null) System.setProperty("jdbc.drivers", drivers); String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); return DriverManager.getConnection(url, username, password); }
30
Statement Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = “jdbc:odbc:mydatabase”; Connection conn = DriverManager.getConnection(url, ”UserName", ”Password"); Statement stmt = conn.createStatement();
31
Executing a statement Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = “jdbc:odbc:mydatabase”; Connection conn = DriverManager.getConnection(url, ”UserName", ”Password"); Statement stmt = conn.createStatement(); String sql = “SELECT * FROM Reservations WHERE (LastName = ‘Burke’);”; stmt.execute (sql);
32
ResultSet Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = “jdbc:odbc:mydatabase”; Connection conn = DriverManager.getConnection(url, ”UserName", ”Password"); Statement stmt = conn.createStatement(); String sql = “SELECT * FROM Reservations WHERE (LastName = ‘Burke’);”; ResultSet rs = stmt.executeQuery (sql); rs.next(); int carType = rs.getInt (“CarType”);
33
Iteration using ResultSet int totalDays = 0; String sql = “SELECT * FROM Reservations;”; ResultSet rs = stmt.executeQuery (sql); while (rs.next()) { totalDays += rs.getInt (“Days”); }
34
Better solution int totalDays = 0; String sql = “SELECT SUM(Days) FROM Reservations;”; ResultSet rs = stmt.executeQuery (sql); rs.next(); totalDays = rs.getInt (1);
35
Example Connection Statement ResultSet Properties / property file
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.