dbmanager java_PowerDesigner9 做出的 DbManager.java (转)

//

// 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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值