实现高效易用的java操作mysql包装[Java编程]
本文“实现高效易用的java操作mysql包装[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
为了简化一些操作,同时为了能兼容其他数据库,因此并没用利用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.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);
}
}
以上是“实现高效易用的java操作mysql包装[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |