📄 tickettag2.java
字号:
/* * TicketTag2.java * * Created on July 13, 2002, 12:38 PM */package com.samspublishing.jpp.ch27;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;import java.io.*;import java.sql.*;import java.util.*;/** * * @author Stephen Potts * @version */public class TicketTag2 extends TagSupport{ private String custIDString; //information about the customer private int custID; private String lastName; private String firstName; //information about the cruise private int cruiseID; private String destination; private String port; private String sailing; private int numberOfTickets; public int doStartTag() { retrieveFromDB(); /*lastName = "Joe"; firstName = "Burns"; //information about the cruise cruiseID = 1001; destination = "Cuba"; port = "Tampa"; sailing = "1/1/03"; numberOfTickets = 4; **/ try { JspWriter out = pageContext.getOut(); out.print("The custID that you entered = " + custID); out.print("<br>" ); out.print("The Last Name = " + lastName); out.print("<br>" ); out.print("The First Name = " + firstName); out.print("<br>" ); out.print("The cruiseID = " + cruiseID); out.print("<br>" ); out.print("The destination = " + destination); out.print("<br>" ); out.print("The port = " + port); out.print("<br>" ); out.print("The sailing = " + sailing); out.print("<br>" ); out.print("The number of tickets = " + numberOfTickets); }catch (Exception e) { System.out.println("Error in TicketTag2 class" + e); } return(SKIP_BODY); } public void setCustID(String id) { this.custIDString=id; this.custID = Integer.parseInt(custIDString); } public String retrieveFromDB() { java.sql.Connection dbConn = null; Statement statement1 = null; String createStatement; String insertStatement; try { // ============== Make connection to database ================== //load the driver class Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Specify the ODBC data source String sourceURL = "jdbc:odbc:TicketRequest"; //get a connection to the database dbConn = DriverManager.getConnection(sourceURL); //If we get to here, no exception was thrown System.out.println("The database connection is " + dbConn); System.out.println("Making connection...\n"); //Create the statement statement1 = dbConn.createStatement(); //Populate the bean String getBeanString = "SELECT * FROM TicketRequest " + "WHERE CustID = " + custID; ResultSet results = statement1.executeQuery(getBeanString); while (results.next()) { lastName = results.getString("lastName"); firstName = results.getString("firstName"); cruiseID = results.getInt("cruiseID"); destination = results.getString("destination"); port = results.getString("port"); sailing = results.getString("sailing"); numberOfTickets = results.getInt("numberOfTickets"); } return "Successful Retrieval"; } catch (Exception e) { System.out.println("Exception was thrown: " + e.getMessage()); return "UnSuccessful Retrieval"; } finally { try { if (statement1 != null) statement1.close(); if (dbConn != null) dbConn.close(); } catch (SQLException sqle) { System.out.println("SQLException during close(): " + sqle.getMessage()); } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -