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

📄 simpleselectclient.java

📁 JAVA用jdbc联接ORACLE的例子。
💻 JAVA
字号:
package examples.jdbc.oracle;

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

/**
 * This simple example shows how to select data and database metadata, using a Statement, 
 * a ResultSet, and a ResultSetMetaDatadata via JDBC; Run the simplesql example before running this example.
 * This example assumes the table <tt>empdemo</tt> exists. This table is created
 * in the simplesql example.
 * <p>
 * This example is run on the command line and is output to
 * System.out.
 *
 * <p><strong>Hint:</strong> Before you can run this example, you must run the simplesql example. If you just completed that
 * example and you still have the command window open, you can skip to <a href="#run">Run the Example</a>.
 * <p><h3>Build the Example</h3>  
 * <ol>
 * <li>In the <font face="Courier New" size = -1>%SAMPLES_HOME%\server\examples\src\examples.properties</font> file, specify a value 
 * for the following properties:
 * <ul>
 * <li>sid - The Oracle System Identifier (SID) of the database. This service must be listed in your tnsnames.ora file.
 * <li>db.user - The user name to use to connect to the database and create
 * tables.
 * <li>db.password - The password to use when connecting to the database.
 * </ul>
 * <p>The ant scripts for building and running the example use properties set
 * in this file.
 * <p>
 * <li> Open a new command shell.
 * <p>
 * <li>Set up this development shell as described in
 * <a href=../../examples.html#environment>Setting up Your Environment for
 * Building and Running the Examples</a>.
 * <p>
 * <li> Change to the <font face="Courier New" size = -1>%SAMPLES_HOME%\server\examples\src\examples\jdbc\oracle</font> directory.
 * <p>
 * 
 * <p>
 * <li>Compile the example by executing the following command:
 * <p>
 * <pre>  prompt&gt;<b> ant</b></pre> 
 * <p>
 * <p><b>Note:</b> This script builds all of the examples in the oracle
 * directory.
 * 
 * </ol>
 * <p><h3><A name="run">Run the Example</a></h3>
 * <p>
 * Execute the following command in your development shell: 
 * 
 * <pre>  prompt&gt;<b> ant run.simple.select</b></pre>
 * <p>
 * <p>You should see the following results:
 * <pre>
 * Buildfile: build.xml
 * 
 * simple.select:
 *      [java] Starting Loading Oracle thin driver .....
 *      [java] Number of Columns: 3
 *      [java] Column Name: EMPID
 *      [java] Nullable: 1
 *      [java] Precision: 38
 *      [java] Scale: 0
 *      [java] Size: 38
 *      [java] Column Type: -5
 *      [java] Column Type Name: NUMBER
 *      [java]
 *      [java] Column Name: NAME
 *      [java] Nullable: 1
 *      [java] Precision: 0
 *      [java] Scale: 0
 *      [java] Size: 30
 *      [java] Column Type: 12
 *      [java] Column Type Name: VARCHAR2
 *      [java]
 *      [java] Column Name: DEPT
 *      [java] Nullable: 1
 *      [java] Precision: 4
 *      [java] Scale: 0
 *      [java] Size: 4
 *      [java] Column Type: 5
 *      [java] Column Type Name: NUMBER
 *      [java]
 * 
 * BUILD SUCCESSFUL
 * 
 * </pre>
 * <h3>There's More</h3>
 *
 * <p>For more information about WebLogic JDBC, see
 * <a href="http://e-docs.bea.com/wls/docs81/jdbc/index.html"><i>Programming WebLogic JDBC</i></a>.
 * <p>
 * @author Copyright (c) 1996-2004 by BEA Systems, Inc.  All Rights Reserved.
 */
public class SimpleSelectClient {
  
  public static void main(String argv[]) throws Exception {
    String user = "scott";
    String password = "tiger";
    String server = "DEMO";

    try {
      for (int i = 0; i < argv.length; i++) 
      {
        if (argv[i].equals("-user")) {
          i++; 
          user = (argv[i].equals("null") ? "" : argv[i]);
        } 
        else if (argv[i].equals("-password")) {
          i++; 
          password = (argv[i].equals("null") ? "" : argv[i]);
        }
        else if (argv[i].equals("-server")) {
          i++; 
          server = (argv[i].equals("null") ? "" : argv[i]);
        }
      }
    } 
    catch(ArrayIndexOutOfBoundsException aiobe) {
      System.err.println("\nUsage: java examples.jdbc.oracle.SimpleSelectClient [options] \n\n" +
                         "where options include:\n" +
                         "    -user <user>            User name to be passed to database.\n" +
                         "    -password <password>    User password to be passed to database.\n" +
                         "    -server <server>        DNS name of database server.\n"); 
      return;
    }

    // Execute example
    run(user, password, server);
  }

  private static void run(String user, String password, String server)
    throws Exception
  {
    Driver myDriver = null;
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    Properties props = new Properties();
    props.put("user",     user);
    props.put("password", password);
    props.put("server",   server);

    try {
      myDriver = (Driver) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
      conn = myDriver.connect("jdbc:oracle:thin:", props);
      stmt = conn.createStatement();

      stmt.execute("select * from empdemo");
      rs = stmt.getResultSet();

      //  ResultSet rs = stmt.executeQuery("select * from empdemo");
      while (rs.next()) {
        System.out.println(rs.getString("empid") + " - " +
                           rs.getString("name")  + " - " +
                           rs.getString("dept"));
      }

      ResultSetMetaData rsmd = rs.getMetaData();

      System.out.println("Number of Columns: " + rsmd.getColumnCount());
      for (int i = 1; i <= rsmd.getColumnCount(); i++) {
        System.out.println("Column Name: "          + rsmd.getColumnName(i));
        System.out.println("Nullable: "             + rsmd.isNullable(i));
        System.out.println("Precision: "            + rsmd.getPrecision(i));
        System.out.println("Scale: "                + rsmd.getScale(i));
        System.out.println("Size: "                 + rsmd.getColumnDisplaySize(i));
        System.out.println("Column Type: "          + rsmd.getColumnType(i));
        System.out.println("Column Type Name: "     + rsmd.getColumnTypeName(i));
        System.out.println("");
      }
    }
    catch (Exception ex) {
      System.out.println("Exception was thrown: " + ex.getMessage());
      throw ex;
    }
    finally { //close connections and statements in a finally block
      try {
        if(rs != null)
          rs.close();
        if(stmt != null)
          stmt.close();
        if(conn != null)
          conn.close();
      }
      catch (SQLException sqle) {
        System.out.println("SQLException was thrown: " + sqle.getMessage());
        throw sqle;
      }
    }
  }
}

⌨️ 快捷键说明

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