The following versions of SqlHelper can support common DDL,DML and query statements, and do not support stored procedures, transactions, etc.
PackageCom.bobo.util;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.Statement;Importjava.util.ArrayList;ImportJava.util.HashMap;Importcom.mysql.jdbc.Connection;ImportJava.sql.*; Public classSqlHelper {//try to figure it out, don't hard code here. Private StaticString drivername = "Com.mysql.jdbc.Driver"; Private StaticString urlname = "Jdbc:mysql://127.0.0.1:3306/bobo_test"; Private StaticString user = "root"; Private StaticString password = "Bobo"; //This class prohibits instantiation of PrivateSqlHelper () {}Private Staticjava.sql.Connection getconnection () {Try{class.forname (drivername); returnDrivermanager.getconnection (urlname, User, password); } Catch(Exception e) {//TODO auto-generated Catch block return NULL; } } Private Static voidPrepareCommand (PreparedStatement pstmt, string[] parms) {Try { if(Parms! =NULL) { for(inti = 0; i < parms.length; i++) { Try{pstmt.setdate (i+ 1, Java.sql.Date.valueOf (Parms[i])); } Catch(Exception e) {Try{pstmt.setdouble (i+ 1, Double.parsedouble (Parms[i])); } Catch(Exception E1) {Try{pstmt.setint (i+ 1, Integer.parseint (Parms[i])); } Catch(Exception E2) {Try{pstmt.setstring (i+ 1, Parms[i]); } Catch(Exception e3) {System.out. Print ("Sqlhelper-preparecommand Err1:" +E3); } } } } } } } Catch(Exception E1) {System.out.print ("Sqlhelper-preparecommand Err2:" +E1); } } /*** For executing statements (insert,update,delete) * *@paramsqltext * SQL statement *@paramparams * Parameter set *@returnthe number of rows affected by the Int,sql statement *@throwsException*/ Public Static intExecuteNonQuery (String sqltext, string[] params)throwsException {preparedstatement ps=NULL; Java.sql.Connection Con=NULL; Try{con=getconnection (); PS=con.preparestatement (SQLTEXT); PrepareCommand (PS, params); returnps.executeupdate (); } Catch(Exception e) {Throw NewException ("ExecuteNonQuery Method Error:" +e.getmessage ()); } } /*** Used to get result set statements (Eg:selete * FROM table) * *@paramcmdtext * SQL statement *@paramparms *@returnArrayList Each element inside is an array of object[columns]@throwsException*/ Public StaticArrayList ExecuteReader (String cmdtext, string[] parms)throwsException {preparedstatement pstmt=NULL; Java.sql.Connection Conn=NULL; Try{conn=getconnection (); Pstmt=conn.preparestatement (Cmdtext); PrepareCommand (pstmt, parms); ResultSet RS=Pstmt.executequery (); ArrayList Al=NewArrayList (); ResultSetMetaData RSMD=Rs.getmetadata (); intColumn =Rsmd.getcolumncount (); while(Rs.next ()) {object[] ob=NewObject[column]; for(inti = 1; I <= column; i++) {Ob[i-1] =Rs.getobject (i); } al.add (OB); } rs.close (); returnal; } Catch(Exception e) {Throw NewException ("Executesqlresultset method Error:" +e.getmessage ()); } finally { Try { if(Pstmt! =NULL) Pstmt.close (); if(Conn! =NULL) Conn.close (); } Catch(Exception e) {Throw NewException ("Executesqlresultset method Error:" +e.getmessage ()); } } } /*** Used to get single field value statements (Specify fields by name) * *@paramcmdtext * SQL statement *@paramName * Column name *@paramparms * oracleparameter[] *@returnobject returns data of type Object *@throwsException*/ Public StaticObject ExecuteScalar (String cmdtext, string name, string[] parms)throwsException {preparedstatement pstmt=NULL; Java.sql.Connection Conn=NULL; ResultSet RS=NULL; Try{conn=getconnection (); Pstmt=conn.preparestatement (Cmdtext); PrepareCommand (pstmt, parms); RS=Pstmt.executequery (); if(Rs.next ()) {returnrs.getobject (name); } Else { return NULL; } } Catch(Exception e) {Throw NewException ("Executesqlobject method Error:" +e.getmessage ()); } finally { Try { if(rs! =NULL) Rs.close (); if(Pstmt! =NULL) Pstmt.close (); if(Conn! =NULL) Conn.close (); } Catch(Exception e) {Throw NewException ("Executesqlobject method Error:" +e.getmessage ()); } } } /*** Used to get single field value statements (Specify fields by ordinal) * *@paramcmdtext * SQL statement *@paramIndex * Column name indexes *@paramparms * oracleparameter[] *@returnObject *@throwsException*/ Public StaticObject ExecuteScalar (String cmdtext,intindex, string[] parms)throwsException {preparedstatement pstmt=NULL; Java.sql.Connection Conn=NULL; ResultSet RS=NULL; Try{conn=getconnection (); Pstmt=conn.preparestatement (Cmdtext); PrepareCommand (pstmt, parms); RS=Pstmt.executequery (); if(Rs.next ()) {returnRs.getobject (index); } Else { return NULL; } } Catch(Exception e) {Throw NewException ("Executesqlobject method Error:" +e.getmessage ()); } finally { Try { if(rs! =NULL) Rs.close (); if(Pstmt! =NULL) Pstmt.close (); if(Conn! =NULL) Conn.close (); } Catch(Exception e) {Throw NewException ("Executesqlobject method Error:" +e.getmessage ()); } } }}
Java version of SqlHelper