Package JDBC, simple and quick use preparedStatement object

zhaozj2021-02-16  102

In the Using the JDBC Operating Database, the most commonly used operation is to increase, delete, change, and check four basic operations in the general Java mode of operation, which is commonly used, using a data object (that is, the database The corresponding data structure, only the SET and GET methods), however, during the write process, when using preparedStatement, the corresponding coupling between the parameters of the object is too strong to write too much SET parameter method, It is very cumbersome. So I wrote a package class for this, which is very simple, is it used to save uncertainty using an object array? parameter. Specific point of view:

/ ** *

/ ** *

Title:

*

Description: Packaging class for the actual operation of the database

*

Copyright: CopyRight (C) 2004

*

COMPANY:

* @Author Chen Shaokun QQ: 29189725

* @version 1.0 dboterator * /

Import java.sql. *;

Public class dboperator {private dbconnectionmanager db = null; private java.sql.preparedStatement ps = null; private java.sql.resultset = null; private connection con = NULL;

Public dboperator () {initialize ();

Private void initialize () {db = dbconnectionmanager.getinstance (); con = db.getConnection ("iDB");}

/ ** * All strings for RESULTSET in database operations for example select * @Param SQL SQL string * @Param params * @return * / public resultSet Select (String SQL, Object [] params) {= null Try {ps = con.preparestatement (SQL); int index = 1; if (params! = Null) {int n = params.length; if (n <2 || n% 2! = 0 || (N / 2)! = This.getstrnum (SQL, "?")) {Throw new illegalargumentException ("The parameter is an odd number or less than 2, or the number of parameters is inconsistent");} for (int i = 0; i

IF ((String) params [i]). TOLOWERCASE (). Equals ("image")) {ps.setbytes (INDEX , (byte []) params [i 1]);}}}}} = PS. EXECUTEQUERY ();} catch (numberXception ex);} catch (sqlexception ex) {ex.printStackTrace ();} return} / ** * All database update operation Contains (Insert Update delete * @Param SQL SQL statement * @Param params parameter array * @Return * / public int update (string sql, object [] params) {int Num = 0; try {ps = con.preparestatement (SQL); INDEX = 1; if (params! = null) {INT N = params.length; if (n <2 || n% 2! = 0 || (N / 2)! = this.getstrnum (SQL, "? ")) {Throw new illegalargumentException (" The parameter is odd or less than 2, or the number of parameters is inconsistent ");} for (int i = 0; i

Params [i 1] = this.pareobjtostr (params [i 1]); // Bar second parameter object Convert to string type

IF ((String) params [i]). TOLOWERCASE (). Equals ("string")) {ps.setstring (index , (string) params [i 1]);} f ((String) params I]). TOLOWERCASE (). Equals ("long")) {ps.setlong (INDEX , long.parselope (String) params [i 1]));} f ((String) params [i]) .tolowercase (). Equals ("int")) {ps.setint (INDEX , Integer.Parseint (String) params [i 1]));}} ((string) params [i]). TOLOWERCASE ). Equals ("Date") {ps.setstring (index , (string) params [i 1]);} f ((string) params [i]). TOLOWERCASE (). Equals ("float") ) {Ps.setfloat (index , float.Parsefloat ((string) params [i 1]));} f ((String) params [i]). TOLOWERCASE (). Equals ("double")) {PS .SETDOUBLE (INDEX , Double.Parsedouble ((String) params [i 1]));} f ((String) params [i]). TOLOWERCASE (). Equals ("image") {ps.setbytes Index , (Byte []) params [i 1]);

}}} Num = ps.executeUpdate ();} catch (numberXception ex) {ex.printStackTrace ();} catch (sqlexception ex) {EX.PRINTSTACKTRACE ();}

Return Num;

}

/ ** * Convert the Object type object according to the corresponding type, return String type * @Param obj * @return Return String Type * / private string Pareobjtostr (Object obj) {if (obj == null) Return NULL; IF Obj instanceof string) Return Obj.toString (); if (integer) r o o) .tostring (); if (obj instanceof long) Return ((long) Obj) .tostring (); if (Obj InstanceOf float) Return ((Float) obj) .tostring (); if (obj INSTANCEOF DOUBLE) RETURN ((Double) obj) .tostring (); if (obj instanceof java.util.date) Return New java.text.SIMPLEDATEFORMAT ("YYYY-MM-DD HH: MM: SS"). Format (java.util.date) obj); return obj.toString ();

/ ** * Number of actual strings included in the original string * @Param SQL original string * @Param Str's string * @return the number of strings included in the string * / Private int getStrnum (String SQL, STRING STR) {INT NUM = 0; int index = SQL.INDEXOF (STR); while (index! = -1) {Num ; index = SQL.Indexof (Str, Index Str ());} return num;}

/ ** * Release database connection resource * / public void freecon () {

Try {if (rs! = null) {r.close ();} if (ps! = null) {ps.close ();} if (con! = null) {DB.FreeConnection ("iDB", con) ;}} Catch (sqlexception ex) {ex.printstacktrace ();}}

Public static void main (string args []) {DBOPERATOR DBE = New DBOPERATOR ();

String SQL = "SELECT * from year_sub_com where id = 4"; dbe.select (sql, null); dbe.freecon ();}}

Use very simple: All queries can use the SELECT method, other increase, delete, change, can use the UPDATE method. Two of these parameters,

One is it? SQL statement, the other is corresponding? The number of object arrays, the number of arrays? 2 times, should have type and value for it. For example 1:

String SQL = "delete from year_performance_basic_target where id =?"; DBOPERATOR DBO = New dboperator (); object [jj); dbo.Update (SQL, PARA); for example 2: 2:

String SQL = "SELECT * FROM_STAT_PERSON WHERE COM_STAT_PERSON WHERE COM_CODE =? And SUM_YEAR =?"; DBOPERATOR DBO = New Dboperator (); Object [] Para = {"String", COM_CODE, "INT", "" Year}; ResultSet RS = dbo.Select (SQL, PARA);

In the later development, I found that if you want to increase, delete, change, operation, and a lot of fields, such as nearly 100 fields,

So it is too much trouble when writing the SQL statement and the Update SQL statement, it is easy to mistake, so I wrote a simple "code generator".

Reduced a lot of cumbersome work in the development, although it is relatively simple, it is really consumed. I will give it in the next article.

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

New Post(0)