750 likes | 891 Vues
Part 5: JDBC Tutorial. CIS 6930-01 – Fall 2000. http://aspen.csit.fsu.edu/it1fall00 Instructors: Geoffrey Fox , Bryan Carpenter Computational Science and Information Technology Florida State University Acknowledgements: Nancy McCracken Syracuse University. Introduction.
E N D
Part 5: JDBC Tutorial CIS 6930-01 – Fall 2000 http://aspen.csit.fsu.edu/it1fall00 Instructors: Geoffrey Fox , Bryan Carpenter Computational Science and Information Technology Florida State University Acknowledgements: Nancy McCracken Syracuse University dbc@csit.fsu.edu
Introduction • JDBC—usually interpreted as an acronym for Java Database Connectivity—was introduced in 1996, and revamped in 1998 for Java 2. • It is an API definition intended for implementation as a Java interface to an SQL database. • SQL (“sequel”) is the Structured Query Language, originally defined by IBM in the 70’s, standardized by ANSI/ISO in 1992. • SQL is in turn based on the relational database model. It is implemented (with much variation) by many vendors of RDBMS (Relational Database Management System) software. • First commercial implementation of SQL: Oracle, 1979. dbc@csit.fsu.edu
SQL dbc@csit.fsu.edu
Relations • Mathematically, a relation is a subset of a product space. Equivalently, it is a set of “tuples” with entries from some fixed domains. • In mathematics, the most important relations tend to be binary relations between entities of the same type: LessThan, on {0, 1, 2, 3} {0, 1, 2, 3}: { (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3) } Contains, on {sunder, sun, sundry, dry} {sunder, sun, sundry, dry}: { (sunder, sunder), (sunder, sun), (sun, sun), (sundry, sundry), (sundry, sun), (sundry, dry), (dry, dry) } SameColor, on {tomato, cherry, banana} {tomato, cherry, banana}: { (tomato, tomato), (tomato, cherry), (cherry, tomato), (cherry, cherry), (banana, banana) } Examples respectively of total order, partial order and equivalence relation. dbc@csit.fsu.edu
General Relations • For databases, we are nearly always interested in n-ary relations between distinct domains, e.g. assume: Login = {wcao, Flora, Fulay, zyr98210, jin0328} LastName = {Cao, Flora, Fulay, Zhang} Dept = {CSIT, EE, CS} then we may define the relation: Students, on Login LastName Dept : { (wcao, Cao, CSIT), (Flora, Flora, EE ), (Fulay, Fulay, CS ), (zyr98210, Zhang, CS ), (jin0328, Zhang, CS ) } dbc@csit.fsu.edu
SQL Tables • SQL is inspired by this mathematical model. • Names are changed, and for pragmatic reasons the SQL model does not try to be mathematically pure. • A relation is replaced by a table. • The individual tuples in the relation are called rows. • The domain sets of the relation are replaced by columnsorattributes. • Typing is not as strict as the mathematical model suggests. We define the allowed values of a column in terms of a limited set of predefined types, rather than actual sets. dbc@csit.fsu.edu
Creating a Table • A possible SQL command to create a table for our Students relation is: CREATE TABLE students ( login VARCHAR(10), lastname VARCHAR(20), dept VARCHAR(5) ) • Things to note: • Case is not significant here. By convention we use upper case for SQL keywords. • White-space, including line-breaks, is not significant. • The CREATE TABLE statement is syntactically like a class definition, defining columns (c.f. fields) and their types. • In this analogy, rows of the table would be like class instances. dbc@csit.fsu.edu
Column Types • There are a limited set of types for column data. • Unfortunately (although there is supposed to be a standard) in practice the available types are completely dependent on the vendor. • In JDBC the types are standardized and include: INTEGER Typically 32-bit integer. FLOAT(N) Floating point with N bits of precision. CHARACTER(N) Fixed length string, N characters. VARCHAR(N) Variable length string, maximum N. BLOB A large binary object. dbc@csit.fsu.edu
SQL*Plus • To create a table using the Oracle SQL*Plus interpreter, type the sqlplus command and enter your Oracle account name and password, then: SQL> create table students ( 2 login varchar(10), 3 lastname varchar(20), 4 dept varchar(5) 5 ) ; Table created • In this interpreter commands are terminated by a semicolon. dbc@csit.fsu.edu
Inserting a Row • The SQL command for adding a row to a table is, e.g.: INSERT INTO students VALUES (‘wcao’, ‘Cao’, ‘CSIT’) • In sqlplus I enter: SQL> insert into students values ( 2 ‘wcao’, 3 ‘Cao’, 4 ‘CSIT’) ; 1 row created. • The following examples assume the other tuples from the Student relation are entered in the same way. dbc@csit.fsu.edu
The SELECT command • To view the Students table I can use the SELECT command: SELECT * FROM students The asterisk is a wildcard that causes all columns to be display. • In sqlplus: SQL> select * from students ; LOGIN LASTNAME DEPT ----------------- ----------------------------- ------------ wcao Cao CSIT Flora Flora EE Fulay Fulay CS zyr98210 Zhang CS jin0328 Zhang CS 5 rows selected dbc@csit.fsu.edu
Displaying Chosen Columns • To limit the set of columns printed, specify them in the SELECT command, e.g.: SELECT login, lastname FROM students • In sqlplus: SQL> select login, lastname from students ; LOGIN LASTNAME ----------------- ----------------------------- wcao Cao Flora Flora Fulay Fulay zyr98210 Zhang jin0328 Zhang 5 rows selected dbc@csit.fsu.edu
Displaying Chosen Rows • To limit the set of rows printed, I add a WHERE clause: SELECT * FROM students WHERE dept=‘CS’ Other kinds of tests that can appear in the WHERE clause will be described later. • In sqlplus: SQL> select * from students where dept=‘CS’ ; LOGIN LASTNAME DEPT ----------------- ----------------------------- ------------ Fulay Fulay CS zyr98210 Zhang CS jin0328 Zhang CS 3 rows selected dbc@csit.fsu.edu
A Second Table • The following examples assume I define a second table by: CREATE TABLE departments ( abbrev VARCHAR(5), name VARCHAR(50) ) and add the rows: { (‘CSIT’, ‘Computational Science and Information Technology’), (‘EE’, ‘Electrical Engineering’), (‘CS’, ‘Computer Science’) } dbc@csit.fsu.edu
Selecting from Multiple Tables • A SELECT command can display data from more than one tables: SELECT * FROM students, departments • By itself this just produces a mess. In sqlplus: SQL> select * from students, departments ; LOGIN LASTNAME DEPT ABBRE ----------------- ----------------------------- ------------ ---------- NAME -------------------------------------------------------------------- wcao Cao CSIT CSIT Computational Science and Information Technology Flora Flora EE CSIT Computational Science and Information Technology Fulay Fulay CS CSIT Computational Science and Information Technology . . . 15 rows selected dbc@csit.fsu.edu
Joins • The previous query returned 15rows. • It simply yielded a “Cartesian product” of the two relations, with every row of students being combined with every row of departments. • In itself this is not a useful result. But is a basis from which to add a WHERE clause to select out some meaningful combinations of values from the two tables. • If two tables appearing in the same statementsharesome identical column names, can disambiguate by using qualified names, e.g.: students.login, students.lastname, students.dept, etc. dbc@csit.fsu.edu
References Between Tables • Here is a meaningful query involving the two tables: SELECT login, name FROM students, departments WHERE dept=abbrev • In sqlplus: SQL> select login, name from students, departments 2 where dept=abbrev ; LOGIN NAME ----------------- -------------------------------------------------------------------------- Fulay Computer Science zyr98210 Computer Science jin0328 Computer Science wcao Computational Science and Information Technology Flora Electrical Engineering 5 rows selected dbc@csit.fsu.edu
Primary Keys and Foreign Keys • This kind of cross-reference is so important that SQL provides syntax to allow automatic “integrity checks” and allow optimizations. • We can change the abbrev column to be a primary key of the departments table by the following SQL command: ALTER TABLE departments ADD PRIMARY KEY (abbrev) • We add a constraint that any allowed value in the dept column of students must be a valid primary key in the departments table by the SQL command: ALTER TABLE students ADD FOREIGN KEY (dept) REFERENCES departments (abbrev) dbc@csit.fsu.edu
Integrity Checks • The system will now forbid addition of values to the dept column of students that do not correspond to values in the abbrev column of departments. • For example, I can try to change the dept column of the row describing wcao by the SQL UPDATE command: UPDATE students SET dept=‘IE’ WHERE login=‘wcao’ • Because of the constraints, Oracle will refuse to make this update. In sqlplus: SQL> update students set dept=‘IE’ where login=‘wcao’ ; ERROR at line 1 : ORA-02291: integrity constraint violated - parent key not found. • Incidentally this example illustrates the use of UPDATE command, which changes attributes of existing rows. dbc@csit.fsu.edu
Other Useful Commands • . . . in addition to CREATE, SELECT and UPDATE, which were illustrated earlier: DELETE FROM table_name WHERE condition Deletes selected rows. DROP TABLE table_name Removes a table. DESCRIBE table_name Describes columns of a table. COMMIT Save changes made in this transaction. ROLLBACK Undo changes made in this transaction. dbc@csit.fsu.edu
Conditions • Conditions in WHERE clauses are Boolean expressions built from: • Comparision operators =, <>, <, <=, >, >= • Boolean operators AND, OR, NOT • The LIKE operator. • The LIKE operator compares a column value to a pattern. • In the pattern the wildcard % represents zero or more characters. • The wildcard _ represents a single character. dbc@csit.fsu.edu
JDBC dbc@csit.fsu.edu
A Simple Example import java.sql.* ; public class ShowStudents { public static void main(String args[]) throws Exception { System.setProperty(“jdbc.drivers”, “oracle.jdbc.driver.OracleDriver”) ; String url = “jdbc:oracle:thin:@sirah.csit.fsu.edu:1521:oralin” ; Connection conn = DriverManager.getConnection(url, “dbc”, “ . . . ”) ; Statement stat = conn.createStatement() ; ResultSet rs = stat.executeQuery(“SELECT * FROM students”) ; while(rs.next()) System.out.println(rs.getString(1) + “ ” + rs.getString(2) + “ ” + rs.getString(3)) ; conn.close() ; } } dbc@csit.fsu.edu
Remarks • To compile and run this example you must have the JDBC driver code on your class path. • On the course hosts, add /usr/oracle/jdbc/lib/classes111.zip to your CLASSPATH. For example you might add the line export CLASSPATH=$CLASSPATH:\ $ORACLE_HOME/jdbc/lib/classes111.zip to the end of you .bashrc file. dbc@csit.fsu.edu
Running ShowStudents • If we run ShowStudents we may see something like: sirah$ java ShowStudents wcao Cao CSIT Flora Flora EE Fulay Fulay CS zyr98210 Zhang CS jin0328 Zhang CS • Effect is essentially like typing the command select * from students ; directly into the SQL*Plus interpreter. dbc@csit.fsu.edu
Classes in the Example • The example introduces the following classes and interfaces from java.sql: DriverManager Manages a set of JDBC drivers. Connection A connection or session with a specific database. Context in which SQL statements are executed and results returned. Statement Object used for executing an SQL statement. ResultSet Provides access to a table of data generated by executing a statement. dbc@csit.fsu.edu
The Driver Manager • The driver manager sits between the JDBC application and one or more JDBC drivers. • A JDBC driver contains vendor-specific code to interface to a particular back-end database. • On initialization, the class DriverManager will try to load driver classes referenced in the jdbc.drivers property. • One can also load JDBC drivers explicitly, e.g.: Class.forName(“oracle.jdbc.driver.OracleDriver”) ; dbc@csit.fsu.edu
Making a Connection • There are several getConnection() methods on DriverManager with different argument lists. The one we will use is: static Connection getConnection(String url, String username, String password) • Before you can use this method, you will, of course, need an account on the database concerned. • The username and password are associated with the database(notyour UNIX account!) dbc@csit.fsu.edu
Database URLs • Ideally the syntax of the URL would follow the normal conventions for Internet URLs: protocol // host : port / name • protocol will be a sub-protocol of jdbc:, e.g. jdbc:oracle:thin: • hostand portare self-explanatory. • nameis the name of the database on the host. • Oracle JDBC URLs follow this general pattern, but they use different separators. • @ and: in place of // and /. dbc@csit.fsu.edu
The Connection Interface • The Connection interface includes many methods. • Our first example only uses two simple ones: Statement createStatement() Creates a Statement object for sending SQL statements to the database. void close() Releases database connection and associated JDBC resources. dbc@csit.fsu.edu
The Statement Interface • Object used for executing an SQL statement. • The most important methods for us will be: ResultSet executeQuery(String sql) throws SQLException Executes an SQL statement that returns a single result set. Typically sql will be a SELECT statement. int executeUpdate(String sql) throws SQLException Executes an SQL statement that returns nothing. Typically sql will be an INSERT, UPDATE or DELETE statement. Result is number rows modified. • Note only one ResultSet can exist per Statement. If you need to interleave queries, use multiple Statement objects. • Other useful methods: • addBatch(), executeBatch() to batch several SQL commands. dbc@csit.fsu.edu
The ResultSet Interface • A result set behaves something like an Enumeration or Iterator. This allows to iterate over the set of rows returned by a query. • The next() method is used to move to the first row, then all subsequent rows. • There is a large number of methods of the form: XXX getXXX(int columnIndex) that extract the contents of a column in the current row. • The one we will use most is getString(), which interprets the column as a String. • Note columnIndex starts at 1, not zero. • ResultSet has many other methods. Many were added in JDBC 2.0. dbc@csit.fsu.edu
Example: Web Front End to SQL • Will now go through a fairly substantial example that combines JDBC with servlets. • This Web application will allow a user to connect to an Oracle account on the server host, and submit arbitrary SQL commands and queries. • Besides introducing a couple of new JDBC features, it will illustrate (in more depth than the vending machine examples) the structure of a Web application involving multiple servlets. dbc@csit.fsu.edu
A First Screen • The introductory screen to our application is a static HTML form, sqlconnect.html: <html> <head> . . . </head> <body> <h1>Connect to Oracle database</h1> <form method=post action=“/dbc/servlet/SQLConnect”> Oracle user name: <input type=text name=username size=20> <p> Oracle password: <input type=password name=password size=20> <p> <input type=submit value=“Connect to database”> </form> </body> </html> dbc@csit.fsu.edu
Remarks • The form prompts for Oracle user name and password. • These are sent using the POST method (for privacy) to the servlet SQLConnect. • Note the form of the action URL. This is a URL to a the servlet context /dbc in the same Web server. dbc@csit.fsu.edu
The SQLConnect servlet public class SQLConnect extends HttpServlet { . . . public void doPost(HttpServletRequest req, HttpServletResponse resp) throws . . . { try { String username = req.getParameter(“username”) ; String password = req.getParameter(“password”) ; HttpSession session = req.getSession(true) ; Connection conn = DriverManager.getConnection(url, username, password) ; session.setAttribute(“connection”, conn) ; resp.sendRedirect( resp.encodeRedirectURL(“/dbc/servlet/SQLCommand”)) ; } catch (SQLException e) { . . . make suitable sendError() call . . . } } } dbc@csit.fsu.edu
Remarks • The essential code creates a servlet session, connects to the database, and stores the Connection object in the servlet session. • This done, the browser is redirected to the SQLCommand servlet. • encodeRedirectURL() is similar to encodeURL(), but specifically intended for use with a sendRedirect() . • We assume that the driver is loaded by a suitable init() method, e.g.: public void init() { Class.forName(“oracle.jdbc.driver.OracleDriver”) ; } and the URL for the database is predefined in a static variableurl. dbc@csit.fsu.edu
The SQLCommand servlet public class SQLCommand extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws . . . { HttpSession session = req.getSession(true) ; Connection conn = (Connection) session.getAttribute(“connection”) ; if(conn == null) . . . session probably timed out: call sendError() . . . else { resp.setContentType(“text/html”) ; PrintWriter out = resp.getWriter() ; out.println(“<html><head></head><body>”) ; . . . print forms . . . out.println(“</body></html>”) ; } } } dbc@csit.fsu.edu
Remarks • The connection is retrieved from the current session, if it exists. • Otherwise the servlet just prints three forms, for SQL queries, SQL action commands, and disconnecting, respectively. . . dbc@csit.fsu.edu
Printing the Forms out.println("<form action=" + resp.encodeURL("/dbc/servlet/SQLQuery") + ">") ; out.println("<h1>SQL <em>query</em></h1>") ; out.println("<textarea name=query cols=60 rows=3></textarea><p>") ; out.println("<input type=submit value=\"Submit Query\">") ; out.println("</form>"); out.println("<form action=" + resp.encodeURL("/dbc/servlet/SQLUpdate") + ">") ; out.println("<h1>SQL <em>update</em></h1>") ; out.println("<textarea name=update cols=60 rows=3></textarea><p>") ; out.println("<input type=submit value=\"Submit Update\">") ; out.println("</form>"); out.println("<form action=" + resp.encodeURL("/dbc/servlet/SQLClose") + ">") ; out.println("Close Oracle Connection:") ; out.println("<input type=submit value=\"Close\">") ; out.println("</form>"); dbc@csit.fsu.edu
Handling Queries • The most complicated servlet in our application is SQLQuery. • This will execute an arbitrary SELECT command, and print out the results as an HTML table. • This presents some special problems because we don’t know in advance how many columns the result table will need, or what is the meaning of the entries. • We can cope with this kind of situation using result set metadata. dbc@csit.fsu.edu
Result Set Metadata • The ResultSet class has a method: ResultSetMetaData getMetaData() that returns a metadata object describing the results. • The only methods on ResultSetMetaData we will use here are: int getColumnCount() returns the number of columns in the result set. String getColumnLabel(int columnIndex) returns a suggested label for use in printouts. dbc@csit.fsu.edu
The SQLQuery servlet public class SQLQuery extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws . . . { HttpSession session = req.getSession(true) ; Connection conn = (Connection) session.getAttribute(“connection”) ; if(conn == null) . . . session probably timed out: call sendError() . . . else { String query = req.getParameter(“query”) ; try { Statement stat = conn.createStatement() ; ResultSet rs = stat.executeQuery(query) ; . . . generate response . . . } catch (SQLException e) { . . . make suitable sendError() call . . . } } } } dbc@csit.fsu.edu
Remarks • This servlet gets the SQL query command from the form data, creates a Statement, and executes the query. • The non-trivial work is in generating the HTML response. . . dbc@csit.fsu.edu
Generating the Response ResultSetMetaData rsmd = rs.getMetaData() ; int cols = rsmd.getColumnCount() ; . . . set response content type, print HTML headers . . . out.println("<table border cellspacing=0 cellpadding=5>") ; out.println("<tr bgcolor=lightblue>") ; // print column headers for(int i = 1 ; i <= cols ; i++) out.println("<td>" + rsmd.getColumnLabel(i) + "</td>") ; out.println("</tr>") ; while(rs.next()) { // print the rows out.println("<tr>") ; for(int i = 1 ; i <= cols ; i++) out.println("<td>" + rs.getString(i) + "</td>") ; out.println("</tr>") ; } out.println("</table>") ; . . . print HTML footers . . . dbc@csit.fsu.edu
Remarks • The complete code generates a couple of form buttons at the end of the HTML page, giving the option to submit another command (go back to the SQLCommand servlet) or close the connection (go to the SQLClose servlet). dbc@csit.fsu.edu
Action Statements • The last two servlets are relatively simple. • SQLUpdate has a similar structure to SQLQuery but it reads the update parameter, and executes the command by: String update = req.getParameter(“update”) ; try { Statement stat = conn.createStatement() ; int rows = stat.executeUpdate(update) ; . . . generate response . . . } catch (. . .) { . . .} • SQLClose calls conn.close() then redirects the browser back to the sqlconnect.html page. dbc@csit.fsu.edu
Submitting a Command dbc@csit.fsu.edu
The Response dbc@csit.fsu.edu
An Elementary “3-tier” Application dbc@csit.fsu.edu