JDBC basedao. Java

Source: Internet
Author: User
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 ();}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.