Docsity

Java DB Connectivity: Lab for Database Management & Info Retrieval Students, Lab Reports of Information Technology

Toggle sidebar
bg1
INFO-340: Database Management and Information Retrieval 1 | 2
Lab 04: Introduction to Java DataBase Connectivity
Learning Objectives
• To introduce JDBC, including connections, statements, and result sets.
• Create, debug and deploy a simple JSP application that uses JDBC.
Background
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]
Resources
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
Using JDBC
JDBC allows you to do three major things:
1. Establish a connection with the database;
2. Submit SQL statements (SELECT, CREATE TABLE, INSERT, UPDATE, etc.) to the database;
3. Receive and process the results (e.g., return results of a SELECT statement). that you can send
Activities
Please Note: To complete this lab you must have 1) finished lab 03 and 2) loaded the data for Assignment
#2 into your database.
1) Navigate to the sample code for lab 04 on the course website. Download the code into your IDE. (The
code is also included at the end of this assignment sheet.)
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?
pf3
pf4
pf5

Partial preview of the text

Download Java DB Connectivity: Lab for Database Management & Info Retrieval Students and more Lab Reports Information Technology in PDF only on Docsity!

INFO-340: Database Management and Information Retrieval 1 | 2 Lab 04: Introduction to Java DataBase Connectivity

Learning Objectives

  • To introduce JDBC, including connections, statements, and result sets.
  • Create, debug and deploy a simple JSP application that uses JDBC.

Background

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]

Resources

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

Using JDBC

JDBC allows you to do three major things:

  1. Establish a connection with the database;
  2. Submit SQL statements (SELECT, CREATE TABLE, INSERT, UPDATE, etc.) to the database;
  3. Receive and process the results (e.g., return results of a SELECT statement). that you can send

Activities

Please Note: To complete this lab you must have 1) finished lab 03 and 2) loaded the data for Assignment #2 into your database.

  1. Navigate to the sample code for lab 04 on the course website. Download the code into your IDE. (The code is also included at the end of this assignment sheet.)

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

  1. 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

  2. Examine the Java Documentation for the following two classes: a. Statement b. ResultSet And determine what these classes do.

  3. 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.

What to submit?

On a lab 03 webpage, please:

  1. Answer the questions from activity #3;
  2. Please include a link to your Web Application from activity #4.

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 / Returns information about the connection / 94 public String connectInfo () 95 { 96 if (conn != null ) 97 return "Connection up! " + getConnInfo (); 98 else 99 return "Connection DOWN " + getConnInfo (); 100 } 101 102 public boolean isConnected () { 103 return (conn == null )? false : true ; 104 } 105 106 / Return the answer and query strings / 107 public String getAnswer () { return answer; } 108 public String getQuery () { return query; } 109 110 / Setting and returning the value of state / 111 public String getStateCode () { return stateCode; } 112 public void setStateCode (String t ) {stateCode = t; } 113 114 public String getStateName () { return stateName; } 115 116 / Make a simple query based on parameter which should be a state code / 117 private String mkQuery (String stateCode) { 118 String whereClause = "WHERE scode = '" + stateCode + "'"; 119 return "SELECT sname FROM state " + whereClause; 120 } 121 122 / Initializes the connection / 123 public void init () { 124 try { 125 / This statement implicitly loads the driver / 126 Class. forName (cDRIVER_STRING); 127 128 / Now, attempt to create a connection */ 129 conn = DriverManager. getConnection (cDB_URL_STRING,cUserID,cUserPassword); 130

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