JDBC II IS 313 1.23.2003 1
Types Java types ≠ SQL types SQL types historical need for backwards compatibility
Numeric SQL types Integers Real numbers tiny int small int integer big int Real numbers real float decimal numeric
More SQL types Boolean String Binary Time bit char varchar long var char Binary binary var binary long var binary Time Date Timestamp
New SQL (99) Types Binary Large Object Character Large Object Array BLOB Character Large Object CLOB Array can manipulate without copying
Types in JDBC Accessors to ResultSet With column name String name = rs.getString (“Name”); With column number int id = rs.getInt(1);
Data accessors
INSERT statement INSERT INTO {table} VALUES ( {value1, …, valuen} ); INSERT INTO Reservations VALUES ( 1212, #1/23/2003#, ‘Sosa’, ‘Sammy’, 2, 14, 2 ); 15
UPDATE statement UPDATE {table} SET {column} = {value} WHERE { criteria } UPDATE Reservations SET RoomType = 4 WHERE ID = 1234; 15
DELETE statement DELETE FROM {table} WHERE { criteria } DELETE FROM Reservations WHERE ID = 9998; 15
JDBC Update queries No ResultSet returned Example 1 Example 2 String sql = “INSERT INTO Reservations “ + “VALUES (100, #12/11/2003#, ‘L.L.’, ‘Bean’, 2, 2, 2);”; int rows = stmt.executeUpdate(sql); // always 1 Example 2 String sql = “DELETE FROM Reservations WHERE (Date = #1/21/2003#);”; int rows = stmt.executeUpdate(sql); // how many rows deleted?
Assembling queries Use String operators (+) to assemble queries String sql = “INSERT INTO Reservations “ + “VALUES (100, #12/11/2003#, “ + firstName + “, “ + lastName + “, 2, 2, 2);”; int rows = stmt.executeUpdate(sql);
Program Development how do I start?
Steps Identify classes Identify properties Identify responsibilities Identify connection / communication Then Top-down elaboration Bottom-up implementation
Identify classes What sorts of “things” present themselves problem description real-world activities
Example Write a program reads a student id # from the command line retrieves student information from a database prints out the student’s enrollment information
Database Students table Enrollments table Id First Name Last Name Enrollment Id Student Id Date Course Number
Identify properties What constitutes the “state” of something? what distinguishes it from other instances? what does it need to “know” in order to function?
Identify responsibilities What does the object do? Always expose properties Other operations?
Identify connection / communication What other objects need to be connected to this one? Examples object A collects other objects object A calls methods of object B object A creates object B object A contains object B
Class design Arrive at Each with a set of classes coherent properties distinct reponsibilities well-defined relationships
Implementation Strategies Top-down Bottom-up work from the problem to the steps of its solution Bottom-up implement the methods of each class
Top-down Write down how the problem can be solved Turn this into Java using the classes you have outlined Turn this into Java then make each line function
Bottom-up For each class write instance variable for properties implement methods