



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




INFO-340: Database Management and Information Retrieval 1 | 2 Lab 04: Introduction to Java DataBase Connectivity
JDBC is a Java Application Programming Interface that enables applications programs to send SQL statements to a relational database and process results. JDBC is a common standard a most relational database systems (e.g., Access, MySQL, Postgresql) provide support for JDBC.
JDBC can be used in a two-tier and three-tier models. Your Social Bookmarking System will be structured in a three-tier model as outlined here:
Client application Å Æ JSP/Java Application Å Æ PostgreSql [Front-end] http [Middle Tier] JDBC [Database]
See chapter 29 in Connolly & Begg, 4th^ Edition for a brief introduction to JDBC.
For an overview http://java.sun.com/products/jdbc/overview.html
API documentation http://java.sun.com/j2se/1.3/docs/api/java/sql/package-summary.html
JDBC allows you to do three major things:
Please Note: To complete this lab you must have 1) finished lab 03 and 2) loaded the data for Assignment #2 into your database.
a. Study the Java and JSP code for this example and answer these questions: i. What does this application do? ii. What do you think the classes Statement and ResultSet are for? iii. What do the methods, next() and getString(1), do?
INFO-340: Database Management and Information Retrieval 2 | 2 Lab 04: Introduction to Java DataBase Connectivity
Create a new Web Application by following these general steps: a. Create a new Web application b. Create a package the Java file (uw.ischool.info340.studentname.) and copy the Java file into that directory c. Edit the file so that it uses your data base on your server d. Copy the JSP file into your web application and update the package path and any other information e. Compile, run and debug your application
Examine the Java Documentation for the following two classes: a. Statement b. ResultSet And determine what these classes do.
Modify your Web Application so that it prompts people to enter a string, either a state code (e.g., WA) or a state name (e.g., Washington). If a state code is entered, then you should return the state name. If a state name is entered, you should return the state code; otherwise, you should return an error.
On a lab 03 webpage, please:
DbConnect.java file:///C:/Documents%20and%20Settings/dhendry/DbConnect.html 64 /* By default, transactions are auto-committed / 65 private Connection conn = null ; 66 67 / A variable for holding a state code -- normally set by client / 68 private String stateCode = "WA"; 69 70 / A variable for holding a state name -- set by query to database / 71 private String stateName = "unknown"; 72 73 / A variable for holding the answer to an SQL query/ 74 private String answer = "no answer"; 75 76 / A variable for holding the query/ 77 private String query = "no query"; 78 79 private String debugString = ""; 80 81 / Returns information about the database connection / 82 public String getConnInfo () 83 { 84 String t = "["; 85 t += "Driver:" + cDRIVER_STRING + ";"; 86 t += "DB URL: " + cDB_URL_STRING + ";"; 87 t += "UserID: " + cUserID + ";"; 88 t += "Password: " + cUserPassword + ";"; 89 t += "] " + debugString; 90 return t; 91 } 92 93 /
DbConnect.java file:///C:/Documents%20and%20Settings/dhendry/DbConnect.html 131 if (conn == null ) 132 throw new Exception ("Could not connect to " + getConnInfo ()); 133 } 134 catch (Exception e) { 135 debugString = e. toString (); 136 e. printStackTrace (); 137 } 138 } 139 140 /* Do a simple query / 141 public void runQuery () { 142 try { 143 if (conn != null ) { 144 / 145 * Create the SQL query that we would like to run 146 / 147 query = mkQuery (stateCode); 148 149 / 150 * The class Statement is Used to send SQL statements to the database 151 / 152 Statement stmt = conn. createStatement (); 153 154 / 155 * The class ResultSet is to hold the results of queries 156 / 157 ResultSet results; 158 159 / 160 * We execute a statement with the following method 161 / 162 results = stmt. executeQuery (query); 163 164 / 165 * Advance to the first record and extract value 166 / 167 if (results. next ()) { 168 / 169 * Retrieve the state name from the record set (a string in field #1) 170 */ 171 stateName = results. getString (1); 172 } 173 else { 174 stateName = "Not found"; 175 } 176 } 177 } 178 catch (Exception e) { 179 debugString = e. toString (); 180 e. printStackTrace (); 181 } 182 } 183 } 184