Package com.Adrop.util;
Import java.sql. *;
PUBLIC CLASS DBMANAGER {
//username
PRIVATE STRING User = ""
//password
Private string password = ""
// host
Private string host = "";
// Database name
Private string database = ""
// private dbmanager dbm = null;
/ *
Private string url = "jdbc: mysql: //" host "/" "useunicode = true & characterencoding = GB2312";
* /
Private string url = "";
Private connection con = NULL;
StMT;
/ **
* Private construction method guarantees that the externally cannot be instantiated, can only be available from DBManager you can
* The instance of it, and can only have one.
* Name the host, database name, database username, and database user passwords.
* @Param Host String
* @Param Database String
* @Param User String
* @Param Password String
* /
Private DBManager (String Host, String Database, String User, String Password) {
THIS.HOST = Host;
THIS.DATABASE = DATABASE;
THIS.USER = User;
this.password = password;
// Show Chinese
THIS.URL = "JDBC: mysql: //" Host "/" Database
"? useunicode = true & characterencoding = GB2312";
Try {
Class.Forname ("org.gjt.mm.mysql.driver");
}
Catch (classnotfoundexception e) {
System.err.println ("Class Not Found:" E.GETMESSAGE ());
}
Try {
Con = DriverManager.getConnection (this.url, this.user, this.password);
/ / Connection type is resultSet.Type_Scroll_insensitive, ResultSet.concur_read_only
Stmt = con.createstatement (ResultSet.Type_Scroll_INSensitive,
ResultSet.concur_read_only);
}
Catch (SQLException a) {
System.err.Println ("SQL Exception:" A.getMessage ());
}
}
/ **
* Static factory method to get a DBManager instance
* /
Public Static dbmanager getInstance (String Host, String Database, String User, String Password) {if (dbm == null) {
DBM = New DBManager (Host, Database, User, Password);
}
Return DBM;
}
/ **
* Returns the connected connection
* /
Public connection getCon () {
Return con;
}
/ **
* Implement a simple query statement
* Returns the result set
* /
Public ResultSet ExecuteQuery (String SQL) {
ResultSet RS = NULL;
Try {
RS = stmt.executequery (SQL);
}
Catch (SQLException E) {
E.PrintStackTrace ();
}
Return RS;
}
/ **
* Implement a simple update statement
* Returns true *
* /
Public Boolean ExecuteUpdate (String SQL) {
Boolean v = false;
Try {
v = stmt.executeUpdate (SQL)> 0? true: false;
}
Catch (SQLException E) {
E.PrintStackTrace ();
}
Finally {
Return V;
}
}
}