Read attribute file, operate database

xiaoxiao2021-03-06  41

Read attribute file, operate database

/ * * Create Date 2005-1-13 * * Todo To change this generated file template, go to * Window - Preference - Java - Code - Code Template * / package com.wen.db;

Import java.sql. *; import com.wen.work.configResource;

/ ** * @Author Explorerwen * * Todo To change this generated type annotation template, go to Window - Preference - Java - Code Style - Code Template * / Public Final Class Dbconnection = NULL; Private STATEMENT STMT; private connection conn; private dbconnection () {stmt = null; rset = null; conn = NULL;}

Public synchronized static dbconnection getdbconnection () {if (dbconn == null) dbconn = new dbconnection (); return dbconn;}

/ ** * returns the connection to the database * * @param realPath * absolute path to the configuration file * @return * @throws Exception * / public Connection getConnection (String realPath) throws Exception {try {ConfigResource.getConfigResource (). SetRealPath (realPath) ; Class.forName ( "com.mysql.jdbc.Driver") newInstance ();... conn = DriverManager.getConnection (ConfigResource .getConfigResource () getProperty ( "dB.Url"), ConfigResource .getConfigResource () getProperty ( " DB.user "), configResource .GetconfigResource (). GetProperty (" db.pass ")); conn.setautocommit (false); returnce (SQLEXCEPTION EX) {EX.PrintStackTrace (); return conn = null; } catCH (Exception ex) {ex.printstacktrace (); return conn = null;}

}

/ ** * Submit conn * * @throws SQLEXCEPTION * / Public void commit () throws sqlexception {conn.commit ();

/ ** * Rollback conn * * @throws sqlexception * / public void rollback () THROWS SQLEXCEPTION {Conn.Rollback ();}

/ ** * query returns ResultSet * * @param query * @return * @throws SQLException * / public ResultSet executeQuery (String query) throws SQLException {try {stmt = conn.createStatement (); rset = stmt.executeQuery (query) ;} Catch (sqlexception e) {system.out.println (query); throw new sqlexception (e.tostring ());} return rset;}

/ * ** Update, returns the number of updated records * * @param query * @return * @throws SQLException * / public int executeUpdate (String query) throws SQLException {stmt = conn.createStatement (); int count = stmt.executeUpdate ( Query; if (stmt! = null) stmt.close (); return count;} / ** * Return to the number of columns * @return * @Throws sqlexception * / public int getColumnCount () throws SQLException {ResultSetMetAData RSMD = rset.getMetaData (); return rsmd.getColumnCount ();} / ** * returns the index column name * @param index * @return * @throws SQLException * / public String getColumnName (int index) throws SQLException {ResultSetMetaData rsmd = rset .getMetaData (); return rsmd.getColumnName (index);} / ** * returns the index column values ​​* @param index * @return * @throws SQLException * / public String getData (int index) throws SQLException {return rset.getString (index) .trim ();} / ** * returns the column value based on the column name * @param columnName * @return * @throws SQLException * / public String getData (String columnName) throws SQLException {return rset.getString (columnName ) .trim ();} / ** * Find the next line of the record set * @Return * @throws sqlexception * / public boolean next () throws sqlexception {returnRN RSET.NEXT ();} / ** * Close record set * @Throws SQLEXCEPTION * / Public void r i () throws sqlexception {if (stmt! = null) stmt.close (); if (RSET! = null) Rset.Close ();} / ** * Close Record Set and Connection * @Throws SQLEXCEPTION * / Public Void Close () THROWS SQLEXCEPTION {IF (stmt! = null) stmt.close (); if (RSET! = null) Rset.Close (); if (conn! = null) conn.close } / ** * Close record set and connection * / protected void finalize () throws throwable {close ();}

}

============================================================================================================================================================================================================= ============== / * * Create Date 2005-15 * * Todo To change the template for this generated file, go to * Window - Preference - Java - Code Style - Code template * / package com.wen.db;

Import java.util. *; import java.sql. *; / ** * @Author Explorerwen * * Todo To change this generated type annotation template, go to * Window - Preferences - Java - Code Template * / public class dbopr {private static dbopr dbopr = null; private dbopr () {} public static dbopr getDBOPR () {if (dbopr == null) DBOPR = new dbopr (); return dbopr;} / ** * Execution Query the return result sets into HashMap * @param sqlStr query profile * @param realPath absolute path * @return HashMap * @throws Exception * / public HashMap runQueryHm (String sqlStr, String realPath) throws Exception {DbConnection dbConn = DbConnection.getDbConnection (); Dbconn.getConnection (RealPath); int color hm = new hashMap (); try {if (dbconn.executeQuery (sqlstr)! = Null && dbconn.next ()) {color = dbconn.getColumnCount ( ); For (int i = 1; i <= colcount; i ) {hm.put (dbconn.getColumnname (i), dbconn.getdata (i));}}}}}}}}}}}}}}}}}}}}}}}}} hm;} catch Sqlexception ex); dbconn.close (); Return hm = null;} catch (exception ex) {ex.printstacktrace (); dB Conn.close (); return hm = null;}} / ** * Execute queries, transform the record set to Vector Back * @Param SQLSTR Query Statement * @Param REALPATH Profile Absolute Path * @Return Vector * @Throws Exception * / public Vector runQueryVc (String sqlStr, String realPath) throws Exception {DbConnection dbConn = DbConnection.getDbConnection (); DbConn.getConnection (realPath); int colCount = 0; Vector vc = new Vector (); HashMap hm = new HashMap () Try {if (dbconn.executeQuery (sqlstr)! = Null) {color = dbconn.getColumnCount (); while (dbconn.next ()) {for (int i = 1; i

) {Hm.put (dbconn.getColumnname (i));} vc.add (hm);}} dbconn.close (); return vc;} catch (sqlexception ex) {EX.PrintStackTrace (); Dbconn.close (); return vc = null;} catch (exception ex) {ex.printstacktrace (); dbconn.close (); returnv = null;}} / ** * Update or delete success Return True otherwise, it returns false * @param sqlStr update or delete statements * @param realPath configuration file absolute path * @return boolean * @throws Exception * / public boolean runUpdate (String sqlStr, String realPath) throws Exception {DbConnection dbConn = DbConnection.getDbConnection ( ); DBCONN.GETCONNECTION (REALPATH); Boolean Flag = false; try {ife (dbconn.executeUpdate (sqlstr)> 0) {dbconn.commit (); flag = true;} else {dbconn.rollback (); flag = false DBCONN.CLOSE (); returnif;} catch (sqlexception ex) {ex.printstacktrace (); dbconn.rollback (); dbconn.close (); returnaf false;} catch (exception ex) {EX.PrintStackTrace ); Dbconn.rollback (); dbconn.close (); returnaf false;}}} ===== ============================================================================================================================================================================================================= ========================= / * * Create Date 2005-1-13 * * Todo To change the template of this generated file, please turn to * Window - Preferences - Java - Code Style - Code Template * / Package CoM.Work;

import java.util.Properties; import java.io.InputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; / ** * @author Administrator * * TODO To change the template for this generated type comment, please turn window to * - Preferences - the Java - style Code - the code template * / public final class ConfigResource {/ ** * constructor * / private static ConfigResource config = null; private String realPath = null; private ConfigResource () {} / * * * returns examples * @return * / public synchronized static ConfigResource getConfigResource () {if (config == null) config = new ConfigResource (); return config;} / ** * set the absolute path * @param realPath * / public void SetRealPath (String realpath) {this.realpath = realpath;} / ** * Nettribute value of the database configuration file * @Param Key configuration file key value * @Return * @throws Exception * / public string getProperty (String Key) THROWS Exception {IF (RealPath == Null || RealPath.equals ("Please set REALPATH attribute"); string path = realpath / web-inf / class / com / work / dbconfig. Properties "; Try {INPUTSTREAM IN = New FileInputStream (PATH); Properties POP = New Properties (); pop.load (in); return pop.getProperty (key);} catch (filenotfoundexception ex) {EXPRINTSTACKTRACE (); return "file did not find exception!";} catch (Exception EX) {EX.PRINTSTACKTRACE (); Return "Read Configuration File Exception!";}} Public static void main (String [] arg) throws exception {system.out.println ("3333");}

} ==================================================== =================================================================== attribute file dbconfig.properties format: # ============================================================================================================================================================================================================= ======= # DB config property # expelorerwen 2005-1-13 # ------------------------------- -------------------------- # 说明: # db.name Database Name # db.user User Name # db.pass user password # == ============================================================================================================================================================================================================= ===== DB.Name = db.user = db.pass =

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

New Post(0)