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

📄 buildemployee.java

📁 大量java源程序
💻 JAVA
字号:
/* * @(#)BuildEmployee * * Copyright (c) 1998 Karl Moss. All Rights Reserved. * * You may study, use, modify, and distribute this software for any * purpose provided that this copyright notice appears in all copies. * * This software is provided WITHOUT WARRANTY either expressed or * implied. * * @author  Karl Moss * @version 1.0 * @date    11Mar98 * */package javaservlets.db;import java.sql.*;/** * <p>This applications builds the sample employee database. */public class BuildEmployee{  String m_tableName = "Employee";  /**    * <p>Main entry point for the application    *    * The usage for the application is:    *    *   BuildEmployee <driver> <URL> [<user>] [<password>]    *    * where    *   driver   = the JDBC driver class name to use (such as    *              sun.jdbc.odbc.JdbcOdbcDriver)    *   URL      = the JDBC connection URL    *   user     = the optional user name    *   password = the optional password    */  public static void main(String args[])    {      String driver = null;      String url = null;      String user = null;      String password = null;      boolean needHelp = false;      // Get the driver name      if (args.length > 0) {        driver = args[0];      }      else {        needHelp = true;      }      // Get the URL      if (args.length > 1) {        url = args[1];      }      else {        needHelp = true;      }      // Get the optional user name      if (args.length > 2) {        user = args[2];      }      // Get the optional password      if (args.length > 3) {        password = args[3];      }      // If the user needs help with the arguments, display it      if (needHelp) {        System.out.println("\nUsage: BuildEmployee <driver> " +                           "<URL> [<user>] [<password>]");      }      else {        BuildEmployee o = new BuildEmployee();        o.build(driver, url, user, password);      }    }  /**    * <p>Build the database table    *    * @param driver JDBC driver class name    * @param url JDBC connection URL    * @param user User name    * @param password User password    */  private void build(String driver, String url,                     String user, String password)    {      java.sql.Connection con = null;      java.sql.PreparedStatement ps = null;      try {        // Attempt to create a new instance of the specified        // JDBC driver. Well behaved drivers will register        // themselves with the JDBC DriverManager when they        // are instantiated        trace("Registering " + driver);        java.sql.Driver d = (java.sql.Driver)          Class.forName(driver).newInstance();        // Create a connection to the given URL        con = java.sql.DriverManager.getConnection(url, user,                                                   password);        // Create a new helper class        SQLHelper helper = new SQLHelper(con);        // Drop the table        trace("Dropping existing " + m_tableName + " table");        helper.drop(m_tableName);        // Setup the columns        ColumnDesc cols[] = new ColumnDesc[4];        cols[0] = new ColumnDesc("Empno", Types.INTEGER);        cols[1] = new ColumnDesc("Name", Types.VARCHAR);        cols[2] = new ColumnDesc("Position", Types.VARCHAR);        cols[3] = new ColumnDesc("Picture", Types.LONGVARBINARY);        // Create the table        trace("Creating table " + m_tableName);        helper.create(m_tableName, cols);        // Add data to the table        ps = helper.prepareInsert(m_tableName, cols);        add(ps, "001", "Nebby K. Nezzer", "President",            "images/nezzer.jpg");        add(ps, "002", "Mr. Lunt", "Foreman",            "images/mrlunt.jpg");        add(ps, "003", "Rack", "Jr. Executive",            "images/bob.jpg");        add(ps, "004", "Shack", "Jr. Executive",            "images/junior.jpg");        add(ps, "005", "Benny", "Jr. Executive",            "images/larry.jpg");        add(ps, "006", "George", "Security Guard",            "images/george.jpg");        add(ps, "007", "Laura", "Delivery Driver",            "images/laura.jpg");      }      catch (Exception ex) {        ex.printStackTrace();      }      finally {        // Make sure we always clean up after ourselves        if (con != null) {          try {            ps.close();            con.close();          }          catch (Exception ex) {          }        }      }    }  /**    * <p>Adds a record to the Employee database with the    * given parameters    *    * @param ps Prepared statement    * @param Empno Employee number    * @param Name Employee name    * @param Position Employee position    * @param Picture Name of an image file that contains the    * employee's picture    */  private void add(java.sql.PreparedStatement ps,                   String Empno, String Name, String Position,                   String Picture)    throws Exception    {      ps.setString(1, Empno);      ps.setString(2, Name);      ps.setString(3, Position);      // Attempt to open an input stream      java.io.FileInputStream in = null;      int fileLen = 0;      java.io.File f = new java.io.File(Picture);      if (f.exists()) {        fileLen = (int) f.length();        in = new java.io.FileInputStream(f);      }      else {        System.out.println("Note: " + Picture + " not found");      }      if (in == null) {        ps.setNull(4, Types.LONGVARBINARY);      }      else {        ps.setBinaryStream(4, in, fileLen);      }      ps.execute();      // Close the input stream if necessary      if (in != null) {        in.close();      }    }  /**    * <p>Trace the given string    */  private void trace(String s)    {      System.out.println("BuildEmployee: " + s);    }}

⌨️ 快捷键说明

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