}
}
public Parameters CreateFixedParms(int size) {
return new FixedParameters(size);
}
}
实现高效易用的java操作mysql包装(2)
时间:2010-12-08 博客园 Birdshover
Data2Object<T>接口负责将ResultSet转换为对象。
package dao;
import java.sql.ResultSet;
import java.sql.SQLException;
public interface Data2Object<T> {
public T DataMap(ResultSet rs) throws SQLException;
}
DatabaseManager负责调用
package dao;
import java.sql.SQLException;
public abstract class DatabaseManager {
public static Database getMySqlDatabase() throws SQLException{
return new MySqlDatabase ("jdbc:mysql://localhost/test? user=root&password=123&useUnicode=true&characterEncoding=U TF-8");
}
}
申明数据库枚举类型
package dao;
public enum DBType {
String,
Int16,
Int32,
Int64,
DateTime,
}
无参数调用的情况:
package bean;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import dao.Data2Object;
import dao.Database;
import dao.DatabaseManager;
import dao.SqlHelper;
public class ListBean {
public List<TestBean> Find() throws SQLException {
String sql = "select * from TestTable";
Database db = DatabaseManager.getMySqlDatabase ();
return db.ExecuteObject(new ListConvertor(), sql, null);
}
public class ListConvertor implements Data2Object<List<TestBean>>{
public List<TestBean> DataMap(ResultSet rs) throws SQLException {
List<TestBean> tests = new ArrayList<TestBean>();
while (rs.next()) {
TestBean bean = new TestBean ();
bean.setId(rs.getInt(1));
bean.setName(rs.getString(2));
tests.add(bean);
}
return tests;
}
}
}
实现高效易用的java操作mysql包装(3)
时间:2010-12-08 博客园 Birdshover
申明参数接口
package dao;
public interface Parameters {
void Add(MySqlParameter parm);
int getLength();
MySqlParameter getParameter(int i);
}
实现该接口
package dao;
public class MySqlParameter {
private DBType type;
private int len;
public MySqlParameter(DBType type, int len, Object value) {
super();
this.type = type;
this.len = len;
this.value = value;
}
/**
* @return the type
*/
public DBType getType() {
return type;
|