Danh mục

Java Server Pages: A Code-Intensive Premium Reference- P6

Số trang: 10      Loại file: pdf      Dung lượng: 193.55 KB      Lượt xem: 6      Lượt tải: 0    
Hoai.2512

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Java Server Pages: A Code-Intensive Premium Reference- P6:Before you begin reading Pure JSP Java Server Pages, you might want to take a look at its basicstructure. This should help you outline your reading plan if you choose not to read the text from cover tocover. This introduction gives you an overview of what each chapter covers.
Nội dung trích xuất từ tài liệu:
Java Server Pages: A Code-Intensive Premium Reference- P6import java.io.*;public class SelectDataApp { public SelectDataApp() { } public void selectData() { Connection con = null; try { // Load the Driver class file Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); // Make a connection to the ODBC datasource Movie Catalog con = DriverManager.getConnection(jdbc:odbc:Movie Catalog, , ); // Create the statement Statement statement = con.createStatement(); // Use the created statement to SELECT the DATA // FROM the Titles Table. ResultSet rs = statement.executeQuery(SELECT * +FROM Titles); // Iterate over the ResultSet while ( rs.next() ) { - 51 - // get the title_name, which is a String System.err.println(Title Name = + rs.getString(title_name)); // get the rating System.err.println(Title Rating = + rs.getString(rating)); // get the price System.err.println(Title Price = + rs.getString(price)); // get the quantity System.err.println(Title Quantity = + rs.getString(quantity)+ ); } // Close the ResultSet rs.close(); System.in.read(); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } catch (SQLException sqle) { System.err.println(sqle.getMessage()); } catch (ClassNotFoundException cnfe) { System.err.println(cnfe.getMessage()); } catch (Exception e) { System.err.println(e.getMessage()); } - 52 - finally { try { if ( con != null ) { // Close the connection no matter what con.close(); } } catch (SQLException sqle) { System.err.println(sqle.getMessage()); } } } public static void main(String[] args) { SelectDataApp selectDataApp = new SelectDataApp(); selectDataApp.selectData(); }}To execute a query, use the same Statement object as in previous examples. You just call a differentmethod. The method to perform a query is executeQuery(). Its signature is listed here:public ResultSet executeQuery(String sql) throws SQLExceptionIt takes a SQL string like the executeUpdate() method. The difference from executeUpdate() is thatexecuteQuery() returns a ResultSet object containing the results of the query. In our example, wepassed it the string SELECT * FROM Titles, which returns a collection of rows resulting from thequery.After we have our ResultSet object returned from executeQuery(), we can iterate over it. Thefollowing code snippet shows how our example processes the query results: - 53 -// Iterate over the ResultSetwhile ( rs.next() ) { // get the title_name, which is a String System.err.println(Title Name = + rs.getString(title_name)); // get the rating System.err.println(Title Rating = + rs.getString(rating)); // get the price System.err.println(Title Price = + rs.getString(price)); // get the quantity System.err.println(Title Quantity = + rs.getString(quantity) + );}// Close the ResultSetrs.close();The first thing you do is call the ResultSet.next() method. This method returns a Boolean value,indicating whether the next row in the set is valid. If it is, we can access that row using the Get accessorsprovided by the ResultSet object. In our example, we use only the getString() method, but they allfunction the same except for their return type. They take a string value representing the name of thecolumn in the table and return the type that is part of their method name. For example, getString()returns a java.lang.String and getInt() returns an int. You can continue iterating over theResultSet until next() returns false; at that point you need to close the ResultSet object. Whenyou execute this application, the results will be similar to the following output:Title Name = The Adventures of Buckaroo BonzaiTitle Rating = PGTitle Price = 19.95Title Quantity = 10Title Name = Saving Private RyanTitle Rating = RTitle Price = 19.95Title Quantity = 12Title Name = So I Married An Axe MurdererTitle Rating = PGTitle Price = 19.95Title Quantity = 15Title Name = Happy GilmoreTitle Rating = PGTitle Price = 19.95Title Quantity = 9Title Name = High Plains DrifterTitle Rating = PGTitle Price = 29.95Title Quantity = 10 - 54 -Title Name = Cape FearTitle Rating = NRTitle Price = 6.99Title Quantity = 21Title Name = The Last EmperorTitle Rating = PGTitle Pric ...

Tài liệu được xem nhiều: