数据库连接工具将结果集封装成map或List

最近做java项目的时候对公司项目的mysql查询框架得到的结果不满意,于是自己就整理了部分资料将数据库查询的结果封装成map或者list。希望对大家和自己有一点帮助

 

1.获取connection工具类

public final class DBUtil {

    private final static String driverName = "com.mysql.jdbc.Driver";    //驱动
    private final static String url = "jdbc:mysql://localhost:3306/testtwo";
    private final static String userName = "root";
    private final static String pwd = "123456";


    public static Connection getMysqlConnection()
    {
        Connection connection = null;
        try{
            Class.forName(driverName);
            connection = DriverManager.getConnection(url,userName,pwd);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return connection;
    }
}

2、废话不多说获取封装结果集的精华代码

public class BaseDao {

    /**
     * 获取list结果集
     * @param sql
     * @return
     */
    public static List> findListBySql(String sql) {
        List> list = new ArrayList>();
        Connection connection = null;
        Statement st = null;
        ResultSet rs = null;
        try
        {
            connection = DBUtil.getMysqlConnection();
            st = connection.createStatement();
            rs = st.executeQuery(sql);
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            while (rs.next()) {
                Map rowData = new HashMap();
                for (int i = 1; i <= columnCount; i++) {
                    rowData.put(md.getColumnName(i), rs.getObject(i));
                }
                list.add(rowData);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(rs,st,connection);
        }
        return list;
    }


    /**
     * 获取map结果集
     * @param sql
     * @return
     */
    public static Map findMapBySql(String sql) {
        Map map = new HashMap<>();
        Connection connection = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            connection = DBUtil.getMysqlConnection();
            st = connection.createStatement();
            rs = st.executeQuery(sql);
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            while (rs.next()) {
                for (int i = 1; i <= columnCount; i++) {
                    map.put(md.getColumnName(i), rs.getObject(i));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(rs,st,connection);
        }
        return map;
    }

    public static void closeResources(ResultSet rs,Statement st,Connection connection)
    {
        try
        {
            if (rs != null)
            {
                rs.close();
            }
            if (st != null)
            {
                st.close();
            }
            if (connection != null)
            {
                connection.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
       List> list = BaseDao.findListBySql("select * from user   ");
        System.out.println(list.get(0).get("name"));
    }
}

 

你可能感兴趣的:(java)