DBManager.java made by PowerDesigner9

zhaozj2021-02-17  49

//// this file was generated by powerdesigner ////// ivportsimport 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 connection () {TRY {// do we need to reconnect ?iff (iSconnected () &&! NeedReco Nnect) {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 ("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); returnifury;}} Public boo lean 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 {needReconnect = true (newUser.equalsIgnoreCase (this.user)!);} 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;

转载请注明原文地址:https://www.9cbs.com/read-28593.html

New Post(0)