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

📄 accessjdbc2.java

📁 java2 primer plus一书源程序
💻 JAVA
字号:
/*
 * AccessJDBC2.java
 *
 * Created on January 21, 2002, 4:12 PM
 */

package com.samspublishing.jpp.ch23;

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


public class AccessJDBC2
{
   
   public  static void main(String argv[])
   throws Exception
   {
      java.sql.Connection dbConn = null;
      Statement statement1 = null;
      String createStatement;
      String insertStatement;
      
      //information about the customer
        int custID;
        String lastName;
        String firstName;
      
      //information about the cruise
        int cruiseID;
        String destination;
        String port;
        String sailing;
      
        int numberOfTickets;
        boolean commissionable;
        boolean approved;
      
      
      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();
         
         ////////////////////////////////////////////////////////////////////////////////
         //                       Create the tables in the database                    //
         ////////////////////////////////////////////////////////////////////////////////
         
         //Add the customer information
         custID  =  12345;
         lastName  =  "Joe";
         firstName  =  "Cocomo";
         cruiseID  =  3001;
         destination  =  "Caribbean";
         port  =  "Miami";
         sailing  =   "1/1/2004";
         numberOfTickets  =  3;
         approved =  true;
         commissionable = true;
         
         insertStatement = "INSERT INTO TicketRequest VALUES(" +
         custID + "," +
         "'" + lastName + "'," +
         "'" + firstName + "'," +
         cruiseID + "," +
         "'" + destination + "'," +
         "'" + port + "'," +
         "'" + sailing + "',"  +
         numberOfTickets + "," +
         approved + "," +
         commissionable + ")";
         System.out.println(insertStatement);
         statement1.executeUpdate(insertStatement);
      } catch (Exception e)
      {
         System.out.println("Exception was thrown: " + e.getMessage());
      } 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 + -