/ ** * When you use it, you only need to change the content in the configuration file! * * Profile Database.property content * jdbc.drivers = sun.jdbc.odbc.jdbcodbcdriver * jdbc.url = jdbc: ODBC : mydata * jdbc.username = system * jdbc.password = manager * * /
Package
PRO03;
// package community.database;
Import
Java.io. *;
Import
Java.util. *;
Import
Java.sql. *;
/ ** * Title: Database Manager * Description: Connect Database * Copyright: Copyright 2002 YaoGuodong * Company: * @author unascribed YaoGuodong Yao Guodong * @version 1.0 * @email net_spirit@163.com * @QQ 16898283 * @ reprint of time please Keep copyright information * /
public
Class
DBManager {
/ ** getConnection () gets the contents of the database for the database, and returns the connection result set @throws @throws ioException @Throws Exception declaration drivers, get the database driver declaration URL, get the URL declaration UserName connected to the remote database, get the connection The username of the remote database declares Password, get the password to connect the remote database @return drivemanger.getConnection (URL, Username, Passowrd); * /
public
Connection getConnection ()
Throws
SQLException, IOException, Exception {Properties PROPS =
New
Properties ();
// String filename = "e: //database.property";
// fileinputstream in = new fileinputstream (filename);
InputStream in = getClass (). GetResourceAsStream
"/DATABASE.PROPERTY"
); Protinload (in); string drivers = props.getproperty
"JDBC.DRIVERS"
);
IF
(drivers! =
NULL
) {System.SetProperty
"JDBC.DRIVES"
, Drivers);} String URL = Props.GetProperty
"jdbc.url"
String username = props.getproperty
"JDBC.USERNAME"
); String password = props.getproperty
"jdbc.password"
Class.Forname (drivers);
Return
Drivermanager.getConnection (URL, UserName, Password);
/ ** DBManager () * Connect Database * / Public
DBManager () {
Try
{Conn = getConnection (); stmt = conn.createstatement (); system.out.println
"Connect Database IS OK!"
}
Catch
(Exception E) {system.out.println (
"Connect Database Failed !:"
E);}}
/ ** ExecuteQuery () method, query SQL result @Param Sqlwords for incoming queries SQL statement @return RS returned Query result set * /
public
ResultSet ExecuteQuery (String Sqlwords) {
THIS
. SQLWORDS = SQLWORDS;
Try
{RS = stmt.executeQuery (SQLWORDS);
Catch
(SQLException ex) {system.out.println
"Execute Query Sql Failed !:"
ex.getMessage ());
Return
RS;
/ ** ExecuteUpdate () method, modify the database record @Param Sqlwords for incoming queries @return True | false * /
public
Boolean
ExecuteUpdate (String Sqlwords) {
THIS
. SQLWORDS = SQLWORDS;
Try
{Stmt.executeUpdate (SQLWORDS);
Return
True
}
Catch
(SQLEXCEPTION EX) {system.err.println
"EXECUTE UPDATE SQL FAILED !:"
ex.getMessage ());
Return
False
}}
/ ** ExecuteInsert () method, insert a new database record @Param SQLWORDS for incoming SQL @return true | false * /
public
Boolean
ExecuteInsert (String Sqlwords) {
THIS
. SQLWORDS = SQLWORDS;
Try
{Stmt.executeUpdate (SQLWORDS);
Return
True
}
Catch
(SQLEXCEPTION EX) {system.err.println
"EXECUTE INSERT SQL FAILED !:"
ex.getMessage ());
Return
False
}}
/ ** Executelete () method, delete database record @Param Sqlwords is incoming deleted SQL @return true | false * /
public
Boolean
Executelete (String Sqlwords) {
THIS
. SQLWORDS = SQLWORDS;
Try
{Stmt.executeUpdate (SQLWORDS);
Return
True
}
Catch
(SQLEXCEPTION EX) {System.err.Println ("Execute delete SQL Failed !:"
ex.getMessage ());
Return
False
}}
/ ** close () method, disconnect the database * @ Return True | false * /
public
Boolean
CLOSE () {
Try
{
IF
(RS! =
NULL
) {R.close ();
IF
(stmt! =
NULL
) {Stmt.close ();
IF
(conn! =
NULL
) {Conn.close ();
Return
True
}
Catch
(Exception E) {system.out.print
"Clost Database Connect Failed !:"
E);
Return
False
}}
/ ** * Declaration Conn * declaration RS result set * declaration STMT tone * declares SQLWORDS keyword * /
CONNECTION CONN =
NULL
ResultSet RS =
NULL
; Statement Stmt =
NULL
;
Private
String SQLWORDS =
NULL
;