PowerDesigner9 做出的 DbManager.java

类别:Java 点击:0 评论:0 推荐:

 

//////////////////////////////////////////////
// This file was generated by PowerDesigner //
//////////////////////////////////////////////

// Package declaration
package db;

// Imports

import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.*;

// The database manager is in charge of loading database driver, connecting to a database,
// performing queries and handling errors using JDBC functions.

public class DbManager {
  // Load JDBC driver

  public boolean loadDriver() {
    try {
      // Force the JDBC driver to be loaded
      error = false;
      errorStr = null;
      String status = "Loading the JDBC driver: " + DEFAULT_JDBCDRIVER + "...";
      showStatus(status);
      Class.forName(DEFAULT_JDBCDRIVER);
      return true;
    }
    catch (Exception e) {
      error = true;
      errorStr = "Unable to load the JDBC driver " + DEFAULT_JDBCDRIVER + ".  " +
          e.toString();
      e.printStackTrace(System.out);
      return false;
    }
  }

  // Connect to the database

  public boolean connect() {
    try {
      // Do we need to reconnect?
      if (isConnected() && !needReconnect) {
        return true;
      }
      // Disconnect the old connection
      if (isConnected()) {
        if (!disconnect()) {
          return false;
        }
      }
      // Generate the URL for the database
      String urlStr = url;
      if (database.length() > 0) {
        if (url.equalsIgnoreCase("jdbc:odbc")) {
          url += ":";
        }
        else {
          url += "/";
        }
        url += database;
      }
      // Set connection properties
      Properties props = new Properties();
      props.put("user", user);
      props.put("password", password);
      // Connect to the database at that URL.
      error = false;
      errorStr = null;
      showStatus("Connecting to: " + url + "...");
      DriverManager.setLoginTimeout(DEFAULT_TIMEOUT);
      connection = DriverManager.getConnection(url, props);
      // Create default statement
      statement = connection.createStatement();
      needReconnect = false;
      return true;
    }
    catch (SQLException sqle) {
      error = true;
      errorStr = sqle.toString() + ".  Restart connection.";
      connection = null;
      return false;
    }
    catch (Exception e) {
      connection = null;
      catchEx(e);
      return false;
    }
  }

  // Disconnect the database

  public boolean disconnect() {
    try {
      error = false;
      errorStr = null;
      closeStatement();
      if (connection != null) {
        connection.close();
        connection = null;
      }
      needReconnect = true;
      return true;
    }
    catch (Exception e) {
      catchEx(e);
      return false;
    }
  }

  // Execute a query

  public boolean executeQuery(String query) {
    if (statement != null) {
      try {
        error = false;
        errorStr = null;
        showStatus("Executing query: " + query);
        statement.setQueryTimeout(DEFAULT_TIMEOUT);
        boolean restype = statement.execute(query);
        return restype;
      }
      catch (SQLException sqle) {
        catchSQLEx(sqle);
        return false;
      }
      catch (Exception e) {
        catchEx(e);
        return false;
      }
    }
    return false;
  }

  // Close the statement

  public boolean closeStatement() {
    try {
      error = false;
      errorStr = null;
      if (statement != null) {
        statement.close();
        statement = null;
      }
      return true;
    }
    catch (Exception e) {
      catchEx(e);
      return false;
    }
  }

  // Catch exception

  public void catchEx(Exception e) {
    e.printStackTrace();
    error = true;
    errorStr = "Unexpected Exception: " + e.toString();
  }

  // Catch SQL exception

  public void catchSQLEx(SQLException sqle) {
    error = true;
    errorStr = sqle.toString() + " Cancelling...";
    try {
      statement.cancel();
    }
    catch (SQLException sqle2) {
      errorStr = sqle2.toString() + " after " + sqle.toString();
    }
  }

  // Test if the database connection is established

  public boolean isConnected() {
    if (connection != null) {
      return true;
    }
    else {
      return false;
    }
  }

  // Get URL

  public String getUrl() {
    return url;
  }

  // Set URL

  public void setUrl(String newUrl) {
    if (!newUrl.equalsIgnoreCase(this.url)) {
      needReconnect = true;
    }
    this.url = newUrl;
  }

  // Get user name

  public String getUser() {
    return user;
  }

  // Set user name

  public void setUser(String newUser) {
    if (!newUser.equalsIgnoreCase(this.user)) {
      needReconnect = true;
    }
    this.user = newUser;
  }

  // Get password

  public String getPassword() {
    return password;
  }

  // Set password

  public void setPassword(String newPassword) {
    if (!newPassword.equalsIgnoreCase(this.password)) {
      needReconnect = true;
    }
    this.password = newPassword;
  }

  // Get database

  public String getDatabase() {
    return database;
  }

  // Set database

  public void setDatabase(String newDatabase) {
    if (!newDatabase.equalsIgnoreCase(this.database)) {
      needReconnect = true;
    }
    this.database = newDatabase;
  }

  // Get statement

  public Statement getStatement() {
    return statement;
  }

  // Set statement

  public Connection getConnection() {
    return connection;
  }

  // Get error text

  public String getErrorStr() {
    return errorStr;
  }

  // Test if there is an error

  public boolean hasError() {
    return error;
  }

  // Display message in the standard output

  private void showStatus(String s) {
    System.out.println(s);
  }

  // Default JDBC driver name.
  // For Sybase jConnect: "com.sybase.jdbc2.jdbc.SybDriver"
  // For JDBC/ODBC: "sun.jdbc.odbc.JdbcOdbcDriver"
  // For Oracle: "oracle.jdbc.driver.OracleDriver"

  static final String DEFAULT_JDBCDRIVER = "com.sybase.jdbc2.jdbc.SybDriver";

  // Time out delay

  static final int DEFAULT_TIMEOUT = 10;

  // Connection object

  private Connection connection = null;

  // Statement object

  private Statement statement = null;

  // Error code

  private boolean error = false;

  // Error text

  private String errorStr = "";

  // JDBC database URL

  private String url = "";

  // User name

  private String user = "";

  // Password

  private String password = "";

  // Database name

  private String database = "";

  // Need reconnect indicator

  private boolean needReconnect = true;

}

本文地址:http://com.8s8s.com/it/it16889.htm