In this project, I am most satisfied with my own package of SqlHelper class, I am proud of myself mainly
I am a beginner, I just started not know can do, I just want to try to do so, the result really can, so I
In my module I encapsulated a sqlhelper, although the following is not very good, but basically to meet the needs,
Because it appears that all classes can be converted to string, a string array is used to supplement the mutable arguments in the SQL statement, but there are
One problem is that the second argument must be null at the time of the call, or a string array, if there is only one argument,
To be passed in as a string array (for example: string []params=n "ew string[]{" xxx "};)
A certain amount of trouble. Since it was not known that the mutable parameter was written like this, replace the following second argument with a string ...
Params, the second parameter can be empty without writing, or write null. In the case of only one value directly
You can write to a string type. No longer necessary to convert to string array type
/**sqlhelper * Creator: Lone wolf * created: 2014-07-17 16:24 */package xxx;import java.io.inputstream;import Java.sql.callablestatement;import java.sql.Connection; Import java.sql.PreparedStatement; Import Java.sql.ResultSet; Import Java.sql.sqlexception;import java.sql.Statement; Import java.util.Properties; Import Javax.sql.datasource;import org.apache.commons.dbcp.BasicDataSourceFactory; Import Com.new_fgw.utils.JdbcUtils; public class sqlhelper{private static Connection conn=null; private static PreparedStatement ps=null; private static ResultSet Rs=null; private static DataSource myDataSource = null; private static CallableStatement callablestatement=null; Static {try {Properties prop=new properties (); InputStream In=jdbcutils.class.getclassloader (). getResourceAsStream ("dbcpconfig.properties"); Prop.load (in); Mydatasource=basicdatasourcefactory.createdatasource (prop); } catch (Exception e) {e.printstacktrace ();}} public static Connection GetConn () throws SQLException {return mydatasource.getconnection ();} public static DataSource Getdatasource () {return mydat Asource; } public static PreparedStatement Getps () {return PS; /** execute an updated SQL statement with parameters * @param SQL * @param parameters * @return */public static Boolean exec Uteupdate (String sql,string []parameters) {Boolean b=false; try {conn =getconn (); PS = conn.preparestatement (SQL); if (Parameters!=null) {for (int i=0;i<parameters.length;i++) {ps.setstring (i+1, PA Rameters[i]); }} int x=ps.executeupdate (); if (x>0) {b=true; }} catch (Exception e) {e.printstacktrace (); throw new RuntimeException (E.getmessage ()); }finally{Sqlhelper.close (RS, ps,conn); } return B; } /** execution of stored procedures with parameters, complete data update * @param SQL * @param parameters SQL * @return */public static Boo Lean executeupdatebyprocedure (String procedure,string []parameters) {Boolean b=false; try {conn =getconn (); CallableStatement =conn.preparecall (procedure); if (Parameters!=null) {for (int i=0;i<parameters.length;i++) {CALLABLESTATEMENT.SETST Ring (i+1, parameters[i]); }} int x=callablestatement.executeupdate (); if (x>0) {b=true; }} catch (Exception e) {e.printstacktrace (); throw new RuntimeException (E.getmessage ()); }finally{Sqlhelper.close (RS, ps,conn); } return B; /** execute query stored procedure with parameters * @param SQL * @param parameters SQL * @return */public stat IC REsultset executequerybyprocedure (String procedure,string []parameters) {try {conn =getconn (); Callablestatement=conn.preparecall (procedure); if (Parameters!=null) {for (int i=0;i<parameters.length;i++) {Callablestatement.set String (i+1, parameters[i]); }} rs=callablestatement.executequery (); } catch (Exception e) {e.printstacktrace (); throw new RuntimeException (E.getmessage ()); }finally{//Sqlhelper.close (RS, ps,conn); } return RS; /** execute query SQL statement with parameters * @param SQL * @param parameters SQL * @return */public static ResultSet executeQuery (String sql,string []parameters) {try {conn = Getconn (); PS = conn.preparestatement (SQL); if (Parameters!=null){for (int i=0;i<parameters.length;i++) {ps.setstring (i+1, parameters[i]); } } //?? Canвt?? rs = Ps.executequery (); } catch (Exception e) {e.printstacktrace (); throw new RuntimeException (E.getmessage ()); } return RS; }/** * Close the corresponding resource * @param RS * @param st * @param conn */public static void Close (ResultSet RS, Statement St, Connection conn) {try {if (rs! = null) Rs.close ()} catch (SQLException e) {e.prints Tacktrace (); The finally {try {if (st! = NULL) St.close ()} catch (SQLException e) {e.printstacktrace ();} finally {try {if (conn!=n ull) {conn.close ();}} catch (Exception e) {e.printstacktrace ();}} } }}
The Dbcpconfig.properties file is as follows:
Driverclassname=com.mysql.jdbc.driver
Url=jdbc:mysql://localhost:3306/databasename
Username=root
Password=password
initialsize=10
Maxactive=50
Maxidle=20
Minidle=5
maxwait=60000
Connectionproperties=useunicode=true;characterencoding=utf-8
Defaultautocommit=true
defaultreadonly= defaulttransactionisolation=read_committed