// java reads the XML file and uses this file to configure the database // ***************************************************** ******************************** ****************** XML file (Database.xml) ************* XML Version = "1.0" encoding = "GB2312"?>
Import org.w3c.dom. *; import javax.xml.parsers. *; / ** * @Author dongyanyong * @version 1.0 * @ read XML file * /
public class ReadFromXml {private Document doc; private DocumentBuilderFactory dbf; private DocumentBuilder db; public ReadFromXml () {try {dbf = DocumentBuilderFactory.newInstance (); db = dbf.newDocumentBuilder (); doc = db.parse ( "database.xml" );} Catch (exception e) {system.out.println ("DB XML Not Found!"); E.PrintStackTrace ();}} // ---------------- ------------------------------------- / / Parameter: Empty return value: DatabaseInfo array public string [] getXmltag () {string [] databaseinfo = new string [14]; try {databaseinfo [0] = doc.getElementsBytagname ("drivername"). Item (0) .Getfirstchild () .getNodeValue ();. databaseinfo [1] = doc.getElementsByTagName ( "username") item (0) .getFirstChild () getNodeValue ();.. databaseinfo [2] = doc.getElementsByTagName ( "password") item ( 0) .GETFIRSTCHILD (). GetnodeValue (); DatabaseInfo [3] = DOC.GETELEMENTSBYTAGNAME ("datebase_url"). Item (0) .GetfirstChild (). GetNodeVal UE (); DatabaseInfo [4] = doc.getElementsBytagname ("maxactive"). Item (0) .GetfirstChild (). getnodeValue (); DatabaseInfo [5] = DOC.GETELEMENTSBYTAGNAME ("maxidle"). Item (0). Getfirstchild (). getnodeValue (); DatabaseInfo [6] = doc.getElementsBytagname ("maxwait"). Item (0) .GetfirstChild (). getnodevalue ();
databaseinfo [7] = doc.getElementsByTagName ( "drivername") item (1) .getFirstChild () getNodeValue ();.. databaseinfo [8] = doc.getElementsByTagName ( "username") item (1) .getFirstChild ().. getNodeValue (); databaseinfo [9] = doc.getElementsByTagName ( "password") item (1) .getFirstChild () getNodeValue ();.. databaseinfo [10] = doc.getElementsByTagName ( "datebase_url") item (1).. . getFirstChild () getNodeValue (); databaseinfo [11] = doc.getElementsByTagName ( "maxactive") item (1) .getFirstChild () getNodeValue ();... databaseinfo [12] = doc.getElementsByTagName ( "maxidle") item (1) .Getfirstchild (). Getnodevalue (); DatabaseInfo [13] = doc.getElementSbyTagname ("maxwait"). Item (1) .Getfirstchild (). GetnodeValue ();} catch (exception e) {system.out. Println ("Read XML Content Error!"); E.PrintStackTrace ();} Return DatabaseInfo;} // -------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------} // ********* ********************************************** ***************** ******* The class for the database (XML read by the application) ********** Package readxml; import java.sql. *; import org.apache.commons.dbcp. *; / ** * @Author dongyanyong * @version 1.0 * @ 类 数据 数据 数据 数据 * / public class poolmanager {Private Static PoolManager Pool = New poolManager (); private baricDataSource BDS = NULL; // Sticker ReadFromL READ1; Dbinfo; // Declaration Class Example ReadFromxml Read1;
Private poolmanager () {bds = new barasicDataSource (); dbinfo = new string [14]; read1 = new readFromL (); dbinfo = read1.getxmltag ();
BDS.SETDRIVERCLASSNAME (Dbinfo [7]); bds.setUsername (Dbinfo [8]); bds.setpassword (dbinfo [9]); bds.seturl (dbinfo [10]); bds.setmaxactive (Integer.Parseint (Dbinfo [ 11]); BDS.SETMAXIDLE (Integer.Parseint (Dbinfo [12])); BDS.SETMAXWAIT (Integer.Parseint (dbinfo [13]));} // ------------ -------------------------------------------------- - public static poolmanager getInstance () {Return pool;} // ------------------------------------ --------------------------- public connection getConnection () throws java.sql.sqlexception {return bds.getConnection ();} // -------------------------------------------------- ------------ Public Static Void Closers (RS! = NULL) {= {r.close ();} catch (java.sql.sqlexception ex) {system. Out.println (ex);}}} // -------------------------------------- ------------------------- Public Static Void Closess (Statement SS) {if (ss! = null) {try {ss.close ();} catch (java.sql.sqlexception ex) {system.out.println (ex);}}} // ------ -------------------------------------------------- -------- Public Static Void CloseConnection (Connection CONN) {if (conn! = Null) {Try {Conn.close ();} catch (java.sql.sqlexception ex) {system.out.println EX);}}} // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------} // ******************************** ********************************************************
Import java.sql. *; import java.lang. *; importXML.PoolManager; / ** * @Author dongyanyong * @Version 1.0 * @ Take the class for the database to operate on the database * / public class executesql {private Connection conn = null, conn1 = null; private preparedStatement ps = null, ps1 = null; private resultset = null; private poolmanager pool = NULL;
Private string message = null; // Receive function Return message
Public executesql () {pool = poolmanager.getinstance ();} // -------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- .println (SQL); Try {conn = pool.getConnection (); ps = conn.preparestatement (SQL); int K = ps.executeUpdate ();} catch (java.sql.sqlexception e) {system.out.print (E); RETURN E.TOString (); finally {poolmanager.closess (ps); poolmanager.closeconnection (conn);} Return "ok";} // ------------- -------------------------------------------------- - private string insertin () {/ * Execute SQL * / String SQL = "INSERT INTO TABLEA VALUES ('A', 'C', 'S', 'S')"; Message = Excutsql (SQL); if (! Message.equals ("ok")) Return Message; Else Return "OK";} // ----------------------------- -------------------------------------------------- -} // ***************************************** ****************************