WEB开发网
开发学院软件开发Java 实现高效易用的java操作mysql包装 阅读

实现高效易用的java操作mysql包装

 2009-11-27 00:00:00 来源:WEB开发网   
核心提示: Data2Object<T>接口负责将ResultSet转换为对象。packagedao;importjava.sql.ResultSet;importjava.sql.SQLException;publicinterfaceData2Object<T>{publicTD

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=UTF-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;
        }
    }
}

上一页  1 2 3 4  下一页

Tags:实现 高效 易用

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接