WEB开发网      婵犻潧鍊婚弲顐︽偟椤栨稓闄勯柦妯侯槸閻庤霉濠婂骸浜剧紒杈ㄥ笚閹峰懘鎮╅崹顐ゆ殸婵炴垶鎸撮崑鎾趁归悩鐑橆棄闁搞劌瀛╃粋宥夘敃閿濆柊锕傛煙鐎涙ê鐏f繝濠冨灴閹啴宕熼鍡╀紘婵炲濮惧Λ鍕叏閳哄懎绀夋繛鎴濈-楠炪垽鎮归崶褍妲婚柛銊ュ缁傚秹鏁撻敓锟� ---闂佹寧娲╅幏锟�
开发学院软件开发Java 实现高效易用的java操作mysql包装 阅读

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

 2009-11-27 00:00:00 来源:WEB开发网 闂侀潧妫撮幏锟�闂佸憡鍨电换鎰版儍椤掑倵鍋撳☉娆嶄沪缂傚稄鎷�婵犫拃鍛粶闁靛洤娲ㄩ埀顒佺⊕閵囩偟绱為敓锟�闂侀潧妫撮幏锟�  闂佺ǹ绻楀▍鏇㈠极閻愬灚瀚梺鍨儏閳ь剙绉归弻銊р偓闈涙啞閻h京鈧敻鍋婇崰鏍х暦閿燂拷
核心提示: 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:实现 高效 易用

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