Java JDBC Feature code encapsulation:
Package com.common.common.util.mysql;
Import java.sql.CallableStatement;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import Java.sql.ResultSetMetaData;
Import java.sql.SQLException;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map; public class Connectiondb {/** * Database driver class name/private static final String DRIVER = "Com.mysql.jdbc.Driv
ER "; /** * Connection string/private static final String urlstr = "Jdbc:mysql://*****?useunicode=true&characterenc
Oding=utf8 ";
/** * User name */private static final String USERNAME = "* * *";
/** * Password/private static final String UserPassword = "* * *";
/** * CREATE database Connection object/private Connection connnection = null; /** * Create PreparedStatement object/Private PreparedStatement PREparedstatement = null;
/** * Create CallableStatement object/private CallableStatement callablestatement = null;
/** * Create result set object/private ResultSet ResultSet = null;
static {try {//Load Database driver Class.forName (DRIVER);
catch (ClassNotFoundException e) {System.out.println ("Load driver error");
System.out.println (E.getmessage ());
/** * Establish database connection * @return database connection/public Connection getconnection () {
try {//Get connection connnection = Drivermanager.getconnection (Urlstr, USERNAME,
UserPassword);
catch (SQLException e) {System.out.println (E.getmessage ());
return connnection;
/** * INSERT Update Delete SQL statement uniform method * @param SQL SQL statement * @param params parameter array, null if no arguments * @return Number of rows affected/public int executeupdate (String sql, object[] params) {//affected number of rows int Affectedl
ine = 0;
try {//Get connection connnection = This.getconnection ();
Call SQL PreparedStatement = connnection.preparestatement (sql);
Parameter assignment if (params!= null) {for (int i = 0; i < params.length; i++) {
Preparedstatement.setobject (i + 1, params[i]);
}//Execute Affectedline = Preparedstatement.executeupdate ();
catch (SQLException e) {System.out.println (E.getmessage ());
finally {//release resource CloseAll ();
return affectedline; /** * SQL query puts query results directly into resultset * @param SQL SQL statement * @param params parameter array, null if no argument Return Result set */private ResultSet executequeryrs (String sql, object[] params) {try {//Get connection
Connnection = This.getconnection ();
Call SQL PreparedStatement = connnection.preparestatement (sql);
Parameter assignment if (params!= null) {for (int i = 0; i < params.length; i++) {
Preparedstatement.setobject (i + 1, params[i]);
}//Execute ResultSet = Preparedstatement.executequery ();
catch (SQLException e) {System.out.println (E.getmessage ());
return resultSet; /** * Gets the result set and places the result in the list * * @param SQL * SQL statement * @return List * Result set */public list<object> excutequery (String sql, object[] params) {/ /Execute SQL Get result set ResultSet rs = executequeryrs (sql, params);
Create ResultSetMetaData object ResultSetMetaData RSMD = null;
Result set column number int columnCount = 0;
try {rsmd = Rs.getmetadata ();
Get the result set column number ColumnCount = Rsmd.getcolumncount ();
catch (SQLException E1) {System.out.println (E1.getmessage ());
//Create list list<object> list = new arraylist<object> (); try {//Saves the resultset result to the list while (Rs.next ()) {map<string, object>
Map = new hashmap<string, object> ();
for (int i = 1; I <= columnCount i++) {map.put (Rsmd.getcolumnlabel (i), rs.getobject (i));
} list.add (map); } catch (SQLException e) {System.out.printlN (e.getmessage ());
Finally {//Close all resources closeall ();
} return list; /** * Stored procedure method with one output parameter * @param SQL stored Procedure statement * @param params parameter array * @param outparampos Output parameter position * @param sqltype output Parameter type * @return The value of the output parameter/public Object excutequery (String sql, object[]
Params,int outparampos, int sqltype) {object = null;
Connnection = This.getconnection ();
try {//call stored procedure callablestatement = connnection.preparecall (sql);
Assign a value to a parameter if (params!= null) {for (int i = 0; i < params.length; i++) {
Callablestatement.setobject (i + 1, params[i]); }//Register output parameter Callablestatement.registeroutparameter (Outparampos,
SqlType);
Perform Callablestatement.execute ();
Get output Parameter object = Callablestatement.getobject (Outparampos);
catch (SQLException e) {System.out.println (E.getmessage ());
finally {//release resource CloseAll ();
return object; /** * Close all resources */private void CloseAll () {//Close result set object if (ResultSet!= nul
L) {try {resultset.close ();
catch (SQLException e) {System.out.println (E.getmessage ());
}///Close PreparedStatement object if (PreparedStatement!= null) {try {
Preparedstatement.close ();
catch (SQLException e) {System.out.println (E.getmessage ()); }///Close CallableStatement Object if(CallableStatement!= null)
{try {callablestatement.close ();
catch (SQLException e) {System.out.println (E.getmessage ()); }///Close Connection object if (connnection!= null) {try {C
Onnnection.close ();
catch (SQLException e) {System.out.println (E.getmessage ());
}
}
}
}
MySQL Instance code calls:
Package com.common.common.util.mysql;
Import java.util.ArrayList;
Import Java.util.Date;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Com.smart.entity.HomeDevice;
Import Com.smart.entity.HomeDeviceAlarm;
Import Com.smart.entity.HomeDeviceAttrStatu;
Import Com.smart.entity.HomeDeviceCommand;
Import Com.smart.entity.HomeDeviceLog; public class Connectiondbutil {public list