The following is the database class, that is, the database connection and operation class:
Package COM. lstrap. util; </P> <p> Import Java. SQL. connection; <br/> Import Java. SQL. date; <br/> Import Java. SQL. drivermanager; <br/> Import Java. SQL. preparedstatement; <br/> Import Java. SQL. resultset; <br/> Import Java. SQL. sqlexception; <br/> Import Java. SQL. statement; </P> <p> public class dB {</P> <p>/* the following fields are defined */<br/> private string url = "JDBC: mySQL: // localhost: 3306/Dao? User = root & Password = 1814 "; // Database URL <br/> private string classname =" com. mySQL. JDBC. driver "; // database driver path <br/> private connection conn = NULL; // declare a connection object <br/> private statement stmt = NULL; // declare a statement object <br/> private preparedstatement pstmt = NULL; // declare a preparedstatement object </P> <p>/* initialize something: load the database driver, and establish the connection */<br/> Public void Init () {<br/> try {<br/> // load the database driver <br/> class. forname (classname ). newinstan Ce (); <br/> // establish a connection, connect to the Database URL specified by the attribute URL, and specify the username and password for logging on to the database <br/> conn = drivermanager. getconnection (URL); <br/> // first obtain the statement object in the initialization method <br/> stmt = Conn. createstatement (resultset. type_scroll_insensitive, resultset. concur_read_only); <br/>} catch (exception e) {<br/> E. printstacktrace (); <br/> system. out. println ("initialization method Init () error. Failed to load the database driver or failed to establish a connection !! "); <Br/>}</P> <p>/* constructor */<br/> Public dB () {<br/> Init (); <br/>}</P> <p> /*************************** **************************** * ***********/</P> <p>/* query the database, returned result set resultset */<br/> Public resultset executequery (string SQL) {<br/> resultset rs = NULL; <br/> try {<br/> rs = stmt.exe cutequery (SQL); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/> system. out. println ("Executequery (string SQL) method error !! "); <Br/>}< br/> Return Rs; <br/>}</P> <p>/* Add, modify, or delete data in the database, returns the number of affected rows */<br/> Public int executeupdate (string SQL) {<br/> int icount = 0; <br/> try {<br/> icount = stmt.exe cuteupdate (SQL); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/> system. out. println ("executeupdate (string SQL) method error !! "); <Br/>}< br/> return icount; <br/>}</P> <p> /*************************** * ***** the following method is for preparedstatement **************************** /</P> <p>/* assign a value to pstmt Based on the SQL statement */<br/> Public void readypreparedstatement (string SQL) {<br/> try {<br/> pstmt = Conn. preparestatement (SQL, resultset. type_scroll_insensitive, resultset. concur_read_only); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/> system. out. An error occurred while println ("readypreparedstatement () method !! "); <Br/>}</P> <p>/* The following describes how to set preparedstatement */<br/> Public void setstring (INT index, string Value) {<br/> try {<br/> pstmt. setstring (index, value); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/> system. out. an error occurred while setting the println ("preparedstatement" setstring !! "); <Br/>}< br/> Public void setint (INT index, int value) {<br/> try {<br/> pstmt. setint (index, value); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/> system. out. an error occurred while setting println ("preparedstatement setint !! "); <Br/>}< br/> Public void setboolean (INT index, Boolean value) {<br/> try {<br/> pstmt. setboolean (index, value); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/> system. out. an error occurred while setting println ("preparedstatement" setboolean !! "); <Br/>}< br/> Public void setdate (INT index, date value) {<br/> try {<br/> pstmt. setdate (index, value); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/> system. out. an error occurred while setting println ("preparedstatement" setdate !! "); <Br/>}< br/> Public void setlong (INT index, long value) {<br/> try {<br/> pstmt. setlong (index, value); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/> system. out. an error occurred while setting the println ("preparedstatement" setlong !! "); <Br/>}< br/> Public void setfloat (INT index, float value) {<br/> try {<br/> pstmt. setfloat (index, value); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/> system. out. an error occurred while setting println ("preparedstatement" setfloat !! "); <Br/>}< br/> Public void setbytes (INT index, byte [] value) {<br/> try {<br/> pstmt. setbytes (index, value); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/> system. out. an error occurred while setting println ("preparedstatement" setbytes !! "); <Br/>}</P> <p>/* query the database and return the result set resultset */<br/> Public resultset executequery () {<br/> If (pstmt! = NULL) {<br/> try {<br/> return pstmt.exe cutequery (); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/> system. out. println ("executequery () method error !! "); <Br/> return NULL; <br/>}< br/>} else {<br/> system. Out. println (" preparedstatement has not been set !! "); <Br/> return NULL; <br/>}</P> <p>/* Add, modify, or delete data in the database, returns the number of affected rows */<br/> Public int executeupdate () {<br/> int icount = 0; <br/> If (pstmt! = NULL) {<br/> try {<br/> icount = pstmt.exe cuteupdate (); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/> system. out. println ("executeupdate () method error !! "); <Br/>}< br/>} else {<br/> system. Out. println (" preparedstatement has not been set !! "); <Br/>}< br/> return icount; <br/>}</P> <p> /*************************** **************************************** ***********************************/< /P> <p>/* close all connections */<br/> Public void close () {<br/> try {<br/> If (stmt! = NULL) {<br/> stmt. Close (); <br/> stmt = NULL; <br/>}< br/> If (pstmt! = NULL) {<br/> pstmt. Close (); <br/> pstmt = NULL; <br/>}< br/> If (Conn! = NULL) {<br/> Conn. close (); <br/>}< br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/> system. out. println ("close all connections method close () Error !! "); <Br/>}</P> <p>
The following is a Java applet for testing the DB class (used to insert a record into the table and query the table after it is inserted ):
Package COM. lstrap. util; </P> <p> public class dbtest {<br/> Public static void main (string [] ARGs) throws exception {<br/> string SQL = "insert into usertbl (ID, username, password) values (?,?,?) "; <Br/> dB DB = new dB (); <br/> dB. readypreparedstatement (SQL); <br/> dB. setint (1, 5); <br/> dB. setstring (2, "Sss"); <br/> dB. setstring (3, "yyy"); <br/> db.exe cuteupdate (); <br/> dB. close (); <br/> dB. init (); <br/> db.exe cutequery ("select * From usertbl"); <br/>}< br/>