Package cn.com. cloud. utils; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. ioexception; import Java. io. inputstream; import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. preparedstatement; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. SQL. statement; import Java. util. properties; import javax. servlet. JSP. jstl. SQL. result; import Java X. servlet. JSP. jstl. SQL. resultsupport; import Org. apache. log4j. logger; import cn.com. agree. web. tapestry. util. systemconfigutil; /*** <DL> * <DT> Dao * @ author henrylee * @ version 1.0.0 * @ date 2011-6-20 02:55:59 */public class basedao {// public static void main (string [] ARGs) {// loadproperties (); //}/*** logger */static logger = logger. getlogger (systemconfigutil. class); Private Static string Drivername = NULL; Private Static string url = NULL; Private Static string username = NULL; Private Static string Pwd = NULL; /*** load the driver through static blocks */static {If (drivername = NULL | url = NULL | username = NULL | Pwd = NULL) {loadproperties ();} loaddriver ();}/*** load configuration */public static void loadproperties () {logger. error ("-----" + basedao. class + "loading JDBC. properties configuration "); properties Properties = new properties (); // read JDBC. prope Rties configuration file inputstream instream = NULL; string appclasspath = basedao. class. getresource ("/"). getpath (); appclasspath = appclasspath. replaceall ("% 20", ""); logger. debug ("----- appclasspath:" + appclasspath); string Path = appclasspath + ".. \ conf \ JDBC. properties "; logger. debug ("----- path:" + path); try {instream = new fileinputstream (PATH);} catch (filenotfoundexception E1) {e1.printstacktrace ();} Try {properties. load (Instream);} catch (ioexception e) {e. printstacktrace (); return;} drivername = properties. getproperty ("JDBC. driverclassname "); url = properties. getproperty ("JDBC. URL "); username = properties. getproperty ("JDBC. username "); Pwd = properties. getproperty ("JDBC. password "); logger. debug ("-----" + basedao. class. getname () + "JDBC read. properties: "+" \ n "+" drivername: "+ drivername +" \ n "+" url: "+ URL +" \ n "+" username: "+ username +" \ n" + "PWD:" + PWD);}/*** load driver */Private Static void loaddriver () {try {class. forname (drivername);} catch (classnotfoundexception e) {e. printstacktrace () ;}}/*** get connection * @ return connection * @ throws sqlexception */public static connection getconn () throws sqlexception {return drivermanager. getconnection (URL, username, PWD);}/*** run add, delete, modify, and @ Param SQL * @ Param ARGs * @ return Boolean */public static Boolean ex Ecutesql (string SQL, object [] ARGs) {Boolean Sign = false; connection conn = NULL; preparedstatement state = NULL; try {conn = getconn (); State = Conn. preparestatement (SQL); If (ARGs! = NULL & args. length> 0) {for (INT I = 0; I <args. length; I ++) {state. setobject (I + 1, argS [I]) ;}} int rows=state.exe cuteupdate (); If (rows> 0) Sign = true;} catch (sqlexception e) {e. printstacktrace ();} finally {closeall (null, state, Conn);} return sign;}/*** execute query * @ Param SQL * @ Param ARGs * @ return javax. servlet. JSP. jstl. SQL. result; */public static result executequery (string SQL, object [] ARGs) {// class provided in jstl, because The two jar packages to import jstl result = NULL; connection conn = NULL; preparedstatement state = NULL; resultset rs = NULL; try {conn = getconn (); State = Conn. preparestatement (SQL); If (ARGs! = NULL & args. length> 0) {for (INT I = 0; I <args. length; I ++) {state. setobject (I + 1, argS [I]) ;}} rs = state.exe cutequery (); Result = resultsupport. toresult (RS);} catch (sqlexception e) {e. printstacktrace ();} finally {closeall (RS, state, Conn);} return result ;} /*** close the resource ** @ Param RS * @ Param state * @ Param conn */public static void closeall (resultset RS, statement state, connection conn) {try {If (RS! = NULL) Rs. Close (); If (State! = NULL) state. Close (); If (Conn! = NULL & conn. isclosed () Conn. Close () ;}catch (sqlexception e) {e. printstacktrace ();}}}