Reprinted [classes that encapsulate Java database operations]

Source: Internet
Author: User

Address: http://blog.csdn.net/sl_say0601/archive/2006/01/16/581027.aspx

/* <Br/> * created on 2005-7-27 <br/> * to change the template for this generated file go to <br/> * WINDOW> references> JAVA> code generation> code and comments <br/> */</P> <p> package dB; </P> <p> Import Java. SQL. connection; <br/> Import Java. SQL. statement; <br/> Import Java. SQL. preparedstatement; <br/> Import Java. SQL. callablestatement; <br/> Import Java. SQL. resultset; <br/> Import Java. SQL. drivermanager; <Br/> Import Java. SQL. sqlexception; <br/> Import Java. SQL. types; </P> <p>/** <br/> * @ author LJP <br/> * to change the template for this generated type comment go to <br/> * WINDOW> references> JAVA> code generation> code and comments <br/> */</P> <p> // This class is used to encapsulate Database Operations <br/> public class dbbean <br/>{</P> <p>/** <br/> */<br/> private string URL; // data source URL <br/> private string driver; // database driver <br/> priv Ate string SQL; // SQL statement <br/> private Boolean isquery = true; // query or update is queried by default. <br/> private connection con; // statement object <br/> private statement Sm; // statement object <br/> private preparedstatement PSM; // prepared statement object <br/> private callablestatement CSM; // statement object for executing the Stored Procedure <br/> private resultset RS; // result set object </P> <p> Public dbbean () <br/>{</P> <p >}</P> <p> // enable database connection <br/> Public void opencon (string _ URL, string _ dri Ver) <br/> {<br/> Sm = This. makestatement (_ URL, _ Driver); <br/>}</P> <p> // used to perform the Select Operation <br/> Public resultset getresult (string _ URL, string _ Driver, string _ SQL) <br/>{< br/> Sm = makestatement (_ URL, _ Driver); <br/> SQL = _ SQL; <br/> return this. runexecute (); <br/>}</P> <p> // used to perform insert, delete, and update Operations <br/> Public void dbupdate (string _ URL, string _ Driver, string _ SQL, Boolean _ isquery) <br/>{< br/> Sm = th Is. makestatement (_ URL, _ Driver); <br/> SQL = _ SQL; <br/> isquery = _ isquery; <br/> This. runexecute (); <br/>}</P> <p> // used to initialize the prepared statement object <br/> Public void dbprepared (string _ URL, string _ driver, string _ SQL) <br/>{< br/> PSM = This. makepreparedstatement (_ URL, _ Driver, _ SQL); <br/> SQL = _ SQL; <br/>}</P> <p> // database operation used to execute the prepared statement <br/> Public void runprepared () <br/>{< br/> try <br/> {<br/> psm.exe cute (); <B R/> system. out. println ("database operations for executing prepared statements"); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("database operation error in executing prepared statement "); <br/>}</P> <p >}< br/> // _ values is used to fill in? Parameter <br/> Public void setpreparedval (object [] _ values) <br/>{< br/> try <br/> {<br/> for (INT I = 0; I <_ values. length; I ++) <br/>{< br/> PSM. setobject (I, _ values [I]); <br/>}< br/> catch (exception E) <br/>{< br/> system. out. println ("An error occurred while setting parameters of the prepared SQL statement "); <br/>}</P> <p >}< br/> // _ values is used to fill in? Parameter <br/> Public void setpreparedvalstring (string [] _ values) <br/>{< br/> try <br/> {<br/> for (INT I = 0; I <_ values. length; I ++) <br/>{< br/> PSM. setstring (I, _ values [I]); <br/>}< br/> catch (exception E) <br/>{< br/> system. out. println ("an error occurred when setting the string type parameter of the prepared SQL statement "); <br/>}</P> <p >}< br/> // _ values is used to fill in? Int type parameter <br/> Public void setpreparedvalint (INT [] _ values) <br/>{< br/> try <br/> {<br/> for (INT I = 0; I <_ values. length; I ++) <br/>{< br/> PSM. setint (I, _ values [I]); <br/>}< br/> catch (exception E) <br/>{< br/> system. out. println ("an error occurred when setting the int type parameter of the prepared SQL statement "); <br/>}</P> <p> // The statement object used to initialize the stored procedure, _ procedurename indicates the name of the stored procedure and the parameter list, for example, "_ test [(?,?)]. ", Outval = "? = "; Sometimes outval =" "; <br/> Public void dbcallable (string _ URL, string _ Driver, string _ procedurename, string _ outval) <br/> {<br/> CSM = This. makecallablestatement (_ URL, _ Driver, _ procedurename, _ outval ); <br/>}</P> <p> // set the return value of the stored procedure <br/> Public void setprocedureout (INT _ sqltype) <br/>{< br/> try <br/>{< br/> CSM. registeroutparameter (0, _ sqltype); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. PR Intln ("an error occurred when setting the return value of a stored procedure "); <br/>}< br/> // obtain the returned value of the stored procedure <br/> Public void getprocedureout () <br/>{< br/> try <br/>{< br/> CSM. getObject (0); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("an error occurred when retrieving the returned value of the stored procedure "); <br/>}</P> <p> // database operation used to execute the Stored Procedure <br/> Public void runcallable () <br/>{< br/> try <br/> {<br/> psm.exe cute (); <br/> system. out. println ("database operations for executing Stored Procedures"); <br/>}< B R/> catch (exception e) <br/>{< br/> system. out. println ("database operation error for executing the stored procedure "); <br/>}</P> <p> // you can use <br/> Public resultset reexecute () <br/>{< br/> If (SQL! = NULL) <br/>{< br/> return this. runexecute (); <br/>}< br/> else <br/>{< br/> system. out. println ("Enter the SQL statement first"); <br/> return NULL; <br/>}</P> <p> // set an SQL statement <br/> Public void setsql (string _ SQL) <br/>{< br/> SQL = _ SQL; <br/>}</P> <p> // set the database operation type <br/> Public void setisquery (Boolean _ isquery) <br/>{< br/> isquery = _ isquery; <br/>}</P> <p> // An error occurred while closing the database connection. <br/> Public void dbclose () <br/>{< br/> Try <br/>{</P> <p> url = NULL; <br/> driver = NULL; <br/> SQL = NULL; <br/> isquery = true; </P> <p> con. commit (); </P> <p> If (RS! = NULL) <br/>{< br/> Rs. Close (); <br/>}</P> <p> If (SM! = NULL) <br/>{< br/> SM. Close (); <br/>}</P> <p> If (con! = NULL) <br/>{< br/> con. close (); <br/>}< br/> system. out. println ("close database connection"); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("database disconnection error"); <br/>}</P> <p> // internal usage, used to create a statement object <br/> private statement makestatement (string _ URL, string _ Driver) <br/>{</P> <p> try <br/> {<br/> // load the database driver <br/> class. forname (_ Driver); <br/> system. out. println ("load database driver"); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("unable to load the database driver"); <br/>}</P> <p> statement Sm; <br/> try <br/> {<br/> // create a database connection statement object <br/> con = drivermanager. getconnection (_ URL); <br/> Sm = con. createstatement (); <br/> system. out. println ("creating database connections"); <br/> return Sm; </P> <p >}< br/> catch (exception E) <br/>{< br/> system. out. println ("An error occurred while creating the database connection statement object"); <br/> return NULL; <br/>}</P> <p> // internal usage, used to create a prepared statement object <br/> private preparedstatement makepreparedstatement (string _ URL, string _ Driver, string _ preparedsql) <br/>{</P> <p> try <br/> {<br/> // load the database driver <br/> class. forname (_ Driver); <br/> system. out. println ("load database driver"); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("unable to load the database driver"); <br/>}</P> <p> preparedstatement PSM; <br/> try <br/> {<br/> // create a database connection statement object <br/> con = drivermanager. getconnection (_ URL); <br/> PSM = con. preparestatement (_ preparedsql); <br/> system. out. println ("creating database connections"); <br/> return PSM; </P> <p >}< br/> catch (exception E) <br/>{< br/> system. out. println ("An error occurred while creating the database connection prepared statement object"); <br/> return NULL; <br/>}</P> <p> // internal usage, create a statement object for executing the Stored Procedure <br/> private callablestatement makecallablestatement (string _ URL, string _ Driver, string _ procedurename, string _ outval) <br/>{</P> <p> try <br/> {<br/> // load the database driver <br/> class. forname (_ Driver); <br/> system. out. println ("load database driver"); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("unable to load the database driver"); <br/>}</P> <p> callablestatement CSM; <br/> try <br/> {<br/> // create a database connection statement object <br/> con = drivermanager. getconnection (_ URL); <br/> CSM = con. preparecall ("{" + _ outval + "call" + _ procedurename + "}"); <br/> system. out. println ("creating database connections"); <br/> return CSM; </P> <p >}< br/> catch (exception E) <br/>{< br/> system. out. println ("An error occurred while creating the statement object executing the stored procedure"); <br/> return NULL; <br/>}</P> <p> // internal usage, run the SQL statement and return the result set <br/> private resultset runexecute () <br/>{< br/> try <br/>{< br/> If (isquery) <br/>{< br/> rsw.sm.exe cutequery (SQL); <br/> system. out. println ("perform database query operations"); <br/> Return Rs; <br/>}< br/> else <br/>{< br/> isquery = true; <br/> sm.exe cuteupdate (SQL); <br/> system. out. println ("perform database update operations"); <br/> return NULL; <br/>}< br/> catch (exception E) <br/>{< br/> system. out. println ("SQL statement execution error"); <br/> return NULL; <br/>}</P> <p >}< br/>/* <br/> * created on 2005-7-27 <br/> * <br/> * to change the template for this generated file go to <br/> * WINDOW> references> JAVA> code generation> code and comments <br/> */<br /> package dB; </P> <p> Import Java. SQL. resultset; </P> <p>/** <br/> * @ author LJP <br/> * to change the template for this generated type comment go to <br/> * WINDOW> references> JAVA> code generation> code and comments <br/> */</P> <p> // This class can help programmers easily complete the database. operation, is the Function Extension of dbbean <br/> public class dbaccess extends dbbean <br/>{</P> <p>/** <br/> */ </P> <p> private string accessurl = "jdbcdbc: coolljpdb "; // data source URL <br/> private string accessdriver =" Sun. JDBC. ODBC. jdbcodbcdriver "; // database driver </P> <p> Public dbaccess () <br/>{</P> <p >}</P> <p> // initialize the data source URL and database driver <br/> Public dbaccess (string _ accessurl, string _ accessdriver) <br/>{< br/> accessurl = _ accessurl; <br/> accessdriver = _ accessdriver; <br/>}</P> <p> // executes the query operation and returns the result set. All elements in the table are returned. <br/> Public resultset doselect (string _ tablename) <br/>{</P> <p> resultset rs = NULL; </P> <p> try <br/> {<br/> string SQL = "select * from" + _ tablename; <br/> rs = super. getresult (accessurl, accessdriver, SQL); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("doselect operation error"); <br/>}</P> <p> Return Rs; </P> <p >}</P> <p> // executes the query operation and returns the result set. The specified element is returned. <br/> Public resultset doselect (string _ tablename, string _ wheresql) // _ wheresql indicates the SQL statement after where <br/>{</P> <p> resultset rs = NULL; </P> <p> try <br/> {<br/> string SQL = "select * from" + _ tablename + "where" + _ wheresql; <br/> rs = super. getresult (accessurl, accessdriver, SQL); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("doselect operation error"); <br/>}</P> <p> Return Rs; </P> <p >}</P> <p> // delete a qualified row <br/> Public void dodelete (string _ tablename, string _ wheresql) <br/> {<br/> try <br/> {<br/> string SQL = "delete * from" + _ tablename + "where" + _ wheresql; <br/> super. dbupdate (accessurl, accessdriver, SQL, false); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("dodelete operation error "); <br/>}</P> <p> // perform the delete operation <br/> Public void dodelete (string _ tablename, string _ names, string _ wheresql) // _ names indicates the field name to be deleted or _ names = "*" <br/>{< br/> try <br/>{< br/> string SQL = "Delete ("+ _ names + ") from "+ _ tablename +" where "+ _ wheresql; <br/> super. dbupdate (accessurl, accessdriver, SQL, false); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("dodelete operation error "); <br/>}</P> <p> // delete all rows <br/> Public void dodelete (string _ tablename) <br/> {<br/> try <br/> {<br/> string SQL = "delete from" + _ tablename; <br/> super. dbupdate (accessurl, accessdriver, SQL, false); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("dodelete operation error "); <br/>}</P> <p> // perform the insert operation <br/> Public void doinsert (string _ tablename, string _ values) // _ values is the value of the element to be imported <br/>{< br/> try <br/>{< br/> string SQL = "insert into" + _ tablename + "values (" + _ values + ") "; <br/> super. dbupdate (accessurl, accessdriver, SQL, false); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("dodelete operation error "); <br/>}</P> <p> // perform the insert operation <br/> Public void doinsert (string _ tablename, string _ names, string _ values) // _ names is the name of the field to be inserted, _ values is the value of the element to be entered <br/>{< br/> try <br/>{< br/> string SQL = "insert into" + _ tablename + "( "+ _ names + ") values ("+ _ values +") "; <br/> super. dbupdate (accessurl, accessdriver, SQL, false); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("dodelete operation error "); <br/>}</P> <p> // perform the modification operation <br/> Public void doupdate (string _ tablename, string _ values, string _ wheresql) // _ values is the element and value to be modified, _ wheresql indicates the SQL statement after where <br/>{< br/> try <br/>{< br/> string SQL = "Update" + _ tablename + "set" + _ values + "where" + _ wheresql; <br/> super. dbupdate (accessurl, accessdriver, SQL, false); <br/>}< br/> catch (exception e) <br/>{< br/> system. out. println ("doupdate operation error"); <br/>}</P> <p>

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/sl_say0601/archive/2006/01/16/581027.aspx

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.