糞?互丼叟喘議java荷恬mysql淫廾
扮寂:2010-12-08 鴬人坩 Birdshover
葎阻酒晒匯乂荷恬?揖扮葎阻嬬惹否凪麿方象垂?咀緩旺短喘聞喘SqlHelper 議侘塀。
宸峪頁倖酒汽庁侏議蝕窟圭塀?短喘追秘方象銭俊学吉坪否。
厘心阻利貧寄何蛍議SqlHelper壓窃侏廬算圭中脅載嗤諒籾?遇拝卦指潤惚聞 喘ArrayList淫廾阻匯和。壓宸戦議淫廾麼勣頁葎阻閲窒宸曾倖諒籾。
遍枠賦苧方象垂俊笥?宸旗燕阻匯倖方象垂嬬序佩議荷恬。
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);
}
糞?乎俊笥議MySql淫廾侘塀?凪糞才SqlHelper餓音謹?
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
|