⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testticketrequest2.java

📁 java2 primer plus一书源程序
💻 JAVA
字号:
/*
 * TestTicketRequest2.java
 *
 * Created on July 9, 2002, 11:22 AM
 */

package ch19;

import java.sql.*;
import java.util.*;

/**
 *
 * @author  Stephen Potts
 * @version
 */
public class TestTicketRequest2 implements java.io.Serializable
{
    //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 TestTicketRequest2()
    {
    }
    
    public String toString()
    {
        String outString;
        outString = "------------------------------------------" + "\n";
        
        //information about the customer
        outString += "custID = " + this.custID + "\n";
        outString += "lastName = " + this.lastName + "\n";
        outString += "firstName = " + this.firstName + "\n";
        outString += "-----------------------------------------" + "\n";
        
        //information about the cruise
        outString += "cruiseID = " + this.cruiseID + "\n";
        outString += "destination = " + this.destination + "\n";
        outString += "port = " + this.port + "\n";
        outString += "sailing = " + this.sailing + "\n";
        outString += "numberOfTickets = " + this.numberOfTickets + "\n";
        
        outString += "-----------------------------------------" + "\n";
        return outString;
    }
    
    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();
            
            custID = 17;
            
            //Populate 
            String getString =
            "SELECT * FROM TicketRequest " +
            "WHERE CustID = " + custID;
            
            ResultSet results = statement1.executeQuery(getString);
            
            while (results.next())
            {
                custID = results.getInt("custID");
                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");
                System.out.println(this);
            }
            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());
            }
        }
        
    }
    
    public static void main(String[] args)
    {
        TestTicketRequest2 ttr = new TestTicketRequest2();
        System.out.println("The contents of the database:");
        System.out.println(ttr.retrieveFromDB());
        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -