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

📄 db.java

📁 jdbc连接db2范例,非常完整,我找了很久,供学习参考
💻 JAVA
字号:

class Db
{
  public String alias;
  public String server;
  public int    portNumber = 0; // 0 indicates legacy type 2 connection,
                                // < 0 use universal type 2 connection
  public String userId;
  public String password;
  public Connection con = null;

  public Db()
  {
  }

  public Db(String argv[]) throws Exception
  {
    if( argv.length > 5 ||
        ( argv.length == 1 &&
          ( argv[0].equals( "?" )               ||
            argv[0].equals( "-?" )              ||
            argv[0].equals( "/?" )              ||
            argv[0].equalsIgnoreCase( "-h" )    ||
            argv[0].equalsIgnoreCase( "/h" )    ||
            argv[0].equalsIgnoreCase( "-help" ) ||
            argv[0].equalsIgnoreCase( "/help" ) ) ) )
    {
      throw new Exception(
        "Usage: prog_name [dbAlias] [userId passwd] (use legacy JDBC type 2 driver)\n" +
        "       prog_name -u2 [dbAlias] [userId passwd] (use universal JDBC type 2 driver)\n" +
        "       prog_name [dbAlias] server portNum userId passwd (use universal JDBC type 4 driver)" );
    }

    switch (argv.length)
    {
      case 0:  // Type 2, use all defaults
        alias = "sample";
        userId = "";
        password = "";
        break;
      case 1:  // Type 2, dbAlias specified or Type 2 Universal
        if (argv[0].equalsIgnoreCase("-u2"))
        {
           alias ="sample";
           portNumber = -1;
        }
        else
        {
           alias = argv[0];
        }

        userId = "";
        password = "";
        break;
      case 2:  // Type 2, userId & passwd specified
        if (argv[0].equalsIgnoreCase("-u2"))
        {
           alias = argv[1];
           userId = "";
           password = "";
           portNumber = -1;
        }
        else
        {
           alias = "sample";
           userId = argv[0];
           password = argv[1];
        }

        break;
      case 3:  // Type 2, dbAlias, userId & passwd specified or Type 2 Universal
        if (argv[0].equalsIgnoreCase("-u2"))
        {
           alias = "sample";
           userId = argv[1];
           password = argv[2];
           portNumber = -1;
        }
        else
        {
           alias = argv[0];
           userId = argv[1];
           password = argv[2];
        }

        break;
      case 4:  // Type 4, use default dbAlias or Type 2 Universal
        if (argv[0].equalsIgnoreCase("-u2"))
        {
           alias = argv[1];
           userId = argv[2];
           password = argv[3];
           portNumber = -1;
        }
        else
        {
           alias = "sample";
           server = argv[0];
           portNumber = Integer.valueOf( argv[1] ).intValue();
           userId = argv[2];
           password = argv[3];
        }
        break;
      case 5:  // Type 4, everything specified
        if (! argv[0].equalsIgnoreCase("-u2"))
        {
           alias = argv[0];
           server = argv[1];
           portNumber = Integer.valueOf( argv[2] ).intValue();
           userId = argv[3];
           password = argv[4];
        }
        break;
    }
  } // Db Constructor

  public Connection connect() throws Exception
  {
    String url = null;

    // In Partitioned Database environment, set this to the node number
    // to which you wish to connect (leave as "0" in non-Partitioned Database environment)
    String nodeNumber = "0";

    Properties props = new Properties();

    if ( portNumber < 0 )
    {
      url = "jdbc:db2:" + alias;
      System.out.println(
        "  Connect to '" + alias + "' database using JDBC Universal type 2 driver." );
      Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
    }
    else if( portNumber == 0 )
    {
      url = "jdbc:db2:" + alias;
      System.out.println(
        "  Connect to '" + alias + "' database using JDBC type 2 driver." );
      Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
    }
    else
    {
      url = "jdbc:db2://" + server + ":" + portNumber + "/" + alias;
      System.out.println(
        "  Connect to '" + alias + "' database using JDBC type 4 driver." );
      Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
    }

    if( null != userId )
    {
      props.setProperty("user", userId);
      props.setProperty("password", password);
    }

    props.setProperty("CONNECTNODE", nodeNumber);

    con = DriverManager.getConnection( url, props );

    // enable transactions
    con.setAutoCommit(false);
    return con;
  } // connect

  public void disconnect() throws Exception
  {
    System.out.println();
    System.out.println("  Disconnect from '" + alias + "' database.");

    // makes all changes made since the previous commit/rollback permanent
    // and releases any database locks currrently held by the Connection.
    con.commit();

    // immediately disconnects from database and releases JDBC resources
    con.close();
  } // disconnect
} // Db

class JdbcException extends Exception
{
  Connection conn;

  public JdbcException(Exception e)
  {
    super(e.getMessage());
    conn = null;
  }

  public JdbcException(Exception e, Connection con)
  {
    super(e.getMessage());
    conn = con;
  }

  public void handle()
  {
    System.out.println(getMessage());
    System.out.println();

    if (conn != null)
    {
      try
      {
        System.out.println("--Rollback the transaction-----");
        conn.rollback();
        System.out.println("  Rollback done!");
      }
      catch (Exception e)
      {
      };
    }
  } // handle

  public void handleExpectedErr()
  {
    System.out.println();
    System.out.println(
      "**************** Expected Error ******************\n");
    System.out.println(getMessage());
    System.out.println(
      "**************************************************");
  } // handleExpectedError
} // JdbcException

⌨️ 快捷键说明

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