a good encapsulation JDBC database class
Package cn.com.util;
Import java.sql.*;
Import java.util.*;
/** * Database Operations Tool Class * * @author Administrator */public class Dbutil {private static Connection con = null;//Connection object private static PreparedStatement PSMT = null;//Precompiled Object private static ResultSet rs = null;//result set object private static CALLABL Estatement CSMT = null;//Process Object/** * Get Connection Object * @return/public static Connection getconnetion () {try {Class.
forname ("Com.mysql.jdbc.Driver");
con = DriverManager getconnection ("Jdbc:mysql://localhost:3306/mytest", "root", "gc7923959");
catch (ClassNotFoundException e) {//TODO auto-generated catch block System.out.println ("Driver not Found");
catch (Exception e) {//TODO auto-generated catch block System.out.println ("Database connection failed");
return con; /** * * @param SQL * @return/public static PreparedStatement getpreparedstatement (String sql) {con = g
Etconnetion ();
try {psmt = con.preparestatement (sql);
catch (SQLException e) { TODO auto-generated Catch block E.printstacktrace ();
return PSMT; /** * Query gets all * @param SQL * @return result set/public static ResultSet Getresultset (String sql) {psmt = Getprepareds
Tatement (SQL);
try {rs = Psmt.executequery ();
catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
} return RS; /** * Update operation * @param SQL * @param params * @return The number of rows affected * * public static int executeupdate (String sql, L
ist<object> params) {int count = 0;
PSMT = getpreparedstatement (sql);
Bindpramas (PSMT, params);
try {count = Psmt.executeupdate ();
catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
finally {CloseAll ();
return count; /** * Query with parameters * @param SQL * @param params * @return result set/public static ResultSet executequery (String sq
L, list<object> params) {PSMT = getpreparedstatement (sql); if (params!= null) {bIndpramas (PSMT, params);
try {rs = Psmt.executequery ();
catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
} System.out.println (PSMT);
Return RS;
public static CallableStatement getcallablestatement (String sql) {try {csmt = Getconnetion (). preparecall (SQL);
catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
return CSMT;
public static Object Executecallobject (String sql, Map<integer, object> params) {Object obj = null;
try {int index = 0;//input parameter subscript csmt = getcallablestatement (sql); for (int i = 1; I <= params.size (); i++) {if (Params.get (i). toString (). Equals ("Integer")) {CSMT.REGISTEROUTP
Arameter (i, Types.integer);
index = i;
else {csmt.setobject (i, Params.get (i));
} csmt.execute ();
obj = Csmt.getobject (index); catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
return obj;
public static void Bindpramas (PreparedStatement psmt, list<object> params) {int index = 0;
if (params!= null) {for (Object p:params) {try {psmt.setobject (index + 1, p);
catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
} index++;
}} public static void CloseAll () {try {if (con!= null) {con.close ();
} if (psmt!= null) {psmt.close ();
} if (rs!= null) {rs.close ();
The catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }
}
}