jdbc—总结

1.封装JDBCUtil:将数据库驱动的加载和连接以及连接的关闭封装。

package com.yf.jdbc.test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtil {
    
    // 读取和处理资源文件中的信息
    static Properties pros = null;
    
    // JDBCUtil加载时
    static {
        pros = new Properties();
        try {
            pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static Connection getMysqlConnection () {
        try {
            Class.forName(pros.getProperty("mysqlDriver"));
            return DriverManager.getConnection(pros.getProperty("mysqlURL"), pros.getProperty("mysqlUser") ,pros.getProperty("mysqlPwd"));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static void close (ResultSet rs, Statement ps, Connection con) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
        
        try {
            if (ps != null) {
                ps.close();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
        
        try {
            if (con != null) {
                con.close();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static void close (Statement ps, Connection con) {
        try {
            if (ps != null) {
                ps.close();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
        
        try {
            if (con != null) {
                con.close();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static void close (Connection con) {
        
        try {
            if (con != null) {
                con.close();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }
}

2.ORM(Object Relation Mapping对象关系映射)基本思想
  - 表结构跟类对应,表中字段和类的属性对应,表中记录和对象对应。

  - 让javabean的属性名和类型尽量和数据库保持一致。

  - 一条记录对应一个对象。将这些查询到的对象放到容器中(List,Set,Map)

  

  1.使用Object[]来封装记录

  

package com.yf.testORM;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.yf.jdbc.test.JDBCUtil;

/**
 * 测试使用Object[]来封装一条记录
 * 使用List<Object[]>来存储多条记录
 * @author ibm
 *
 */
public class Demo01 {
    public static void main(String[] args) {
        Connection con = JDBCUtil.getMysqlConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Object[]> objs = null;
        try {
            ps = con.prepareStatement("select empname,salary,age from emp");
            rs = ps.executeQuery();
            objs = new ArrayList<Object[]>();
            Object[] obj = null;
            while(rs.next()) {
                obj = new Object[3];
                obj[0] = rs.getString(1);
                obj[1] = rs.getDouble(2);
                obj[2] = rs.getInt(3);
                objs.add(obj);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, ps, con);
        }
        
        for(Object[] o : objs) {
            System.out.println("" + o[0] + "--" + o[1] + "--" + o[2]);
        }
    }
}

  2.利用Map来封装记录

  

package com.yf.testORM;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.yf.jdbc.test.JDBCUtil;

/**
 * 测试使用Map来封装一条记录
 * 使用List<Map>来存储多条记录
 * @author ibm
 *
 */
public class Demo02 {
    public static void main(String[] args) {
        Connection con = JDBCUtil.getMysqlConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Map<String, Object>> mapInfos = null;
        try {
            ps = con.prepareStatement("select empname,salary,age from emp");
            rs = ps.executeQuery();
            mapInfos = new ArrayList<Map<String, Object>>();
            Map<String, Object> mapInfo = null;
            while(rs.next()) {
                mapInfo = new HashMap<String, Object>();
                mapInfo.put("empname", rs.getString(1));
                mapInfo.put("salary", rs.getDouble(2));
                mapInfo.put("age", rs.getInt(3));
                mapInfos.add(mapInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, ps, con);
        }
        
        for (Map<String, Object> items : mapInfos) {
            for (Entry<String, Object> item : items.entrySet()) {
                System.out.print(item.getKey() + "--" + item.getValue() + "\t");
            }
            System.out.println();
        }
    }
}

  3.利用javabean来封装数据,具体代码和前面两个一样,只不过换成javabean,在这里就不详细记述了。

  

posted @ 2017-05-16 22:21  noooooob  阅读(166)  评论(0编辑  收藏  举报