In order to simplify some operations and to be compatible with other databases, it does not use the form of SqlHelper.
This is just a simple model of the development of the way, not to include data connection pool and other content.
I have seen most of the sqlhelper on the Internet are very problematic in terms of type conversion, and the return result is packaged with ArrayList. The packaging here is mainly to avoid these two problems.
First, the database interface is declared, which represents the operation that a database can perform.
package dao;
import java.sql.SQLException;
public interface Database {
int ExecuteNoneQuery(String cmdtext, Parameters parms) throws SQLException;
<T> T ExecuteObject(Data2Object<T> convertor, String cmdtext,
Parameters parms) throws SQLException;
Object ExecuteScalar(String cmdtext, Parameters parms) throws SQLException;
Parameters CreateFixedParms(int size);
}
The implementation of the interface of the MySQL packaging form, in fact, and SqlHelper almost:
Package DAO;
import java.sql.Connection;
import java.sql.Date;
import Java.sql.DriverManager;
import java.sql.PreparedStatement;
import Java.sql.ResultSet;
Import java.sql.SQLException;
public class Mysqldatabase implements Database {
private Connection Conn;
public mysqldatabase (String connstring) throws SQLException {
conn = drivermanager.getconnection (connstring);
}
public int Executenonequery (String cmdtext, Parameters parms)
throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = conn.preparestatement (Cmdtext);
PrepareCommand (pstmt, parms);
return pstmt.executeupdate ();
} catch (Exception ex) {
} finally {
if (pstmt!= null) {
pstmt.clearparameters ();
Pstmt.close ();
}
if (conn!= null)
Conn.close ();
}
return-1;
}
public <T> T executeobject (data2object<t> convertor, String Cmdtext,
Parameters parms) throws SQLException {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.preparestatement (Cmdtext);
PrepareCommand (pstmt, parms);
rs = Pstmt.executequery ();
return convertor. Datamap (RS);
} catch (Exception ex) {
} finally {
if (Rs!= null)
Rs.close ();
if (pstmt!= null)
Pstmt.close ();
if (conn!= null)
Conn.close ();
}
return null;
}
public Object ExecuteScalar (String cmdtext, Parameters parms)
throws SQLException {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.preparestatement (Cmdtext);
PrepareCommand (pstmt, parms);
rs = Pstmt.executequery ();
if (Rs.next ()) {
return Rs.getobject (1);
} else {
return null;
}
} catch (Exception e) {
} finally {
if (Rs!= null)
Rs.close ();
if (pstmt!= null)
Pstmt.close ();
if (conn!= null)
Conn.close ();
}
return null;
}
private void PrepareCommand (PreparedStatement pstmt, Parameters parms)
throws SQLException {
if (parms!= null && parms.getlength () > 0) {
for (int i = 0; i < parms.getlength (); i++) {
mysqlparameter parm = Parms.getparameter (i);
String value = Parm.getvalue (). toString ();
switch (Parm.gettype ()) {
Case String:
pstmt.setstring (i + 1, value);
Break
Case Int16:
Pstmt.setshort (i + 1, short.parseshort (value));
break;
Case INT32:
Pstmt.setint (i + 1, integer.parseint (value));
break;
Case Int64:
Pstmt.setlong (i + 1, Long.parselong (value));
break;
Case DateTime:
pstmt.setdate (i + 1, date.valueof (value));
break;
Default:
Pstmt.setobject (i + 1, value);
break;
}
}
}
}
Static {
try {
class.forname ("Com.mysql.jdbc.Driver"). newinstance ();
} catch (Exception ex) {
}
}
public Parameters createfixedparms (int size) {
return new fixedparameters (size);
}
}