码迷,mamicode.com
首页 > 数据库 > 详细

jdbc零散知识点

时间:2014-10-29 23:54:26      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   ar   for   sp   数据   

1.可滚动结果集:

 1 conn = JdbcUtils.getConnection();
 2             st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
 3                     ResultSet.CONCUR_READ_ONLY);
 4             rs = st
 5                     .executeQuery("select id, name, money, birthday  from user");
 6             while (rs.next()) {
 7                 System.out.println(rs.getObject("id") + "\t"
 8                         + rs.getObject("name") + "\t"
 9                         + rs.getObject("birthday") + "\t"
10                         + rs.getObject("money"));
11             }
12 
13             System.out.println("------------");
14             rs.absolute(150);//直到特定的行数
15             int i = 0;
16             while (rs.next() && i < 10) {
17                 i++;
18                 System.out.println(rs.getObject("id") + "\t"
19                         + rs.getObject("name") + "\t"
20                         + rs.getObject("birthday") + "\t"
21                         + rs.getObject("money"));
22             }
23 
24              if (rs.previous()){
          }

 2.元数据信息(对于hibernate的实施特别有帮助):

1 Connection conn = JdbcUtils.getConnection();
2         DatabaseMetaData dbmd = conn.getMetaData();
3         System.out.println("db name: " + dbmd.getDatabaseProductName());
4         System.out.println("tx: " + dbmd.supportsTransactions());
5         conn.close();

 3.元数据信息(灵活性特别高)(替换占位符):

Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement(sql);
//            ParameterMetaData pmd = ps.getParameterMetaData();
//            int count = pmd.getParameterCount();
            for (int i = 1; i <= params.length; i++) {//可以为count
                ps.setObject(i, params[i - 1]);
            }

            rs = ps.executeQuery();

            while (rs.next()) {
                System.out.println(rs.getInt("id") + "\t"
                        + rs.getString("name") + "\t" + rs.getDate("birthday")
                        + "\t" + rs.getFloat("money"));
            }

        } finally {
            JdbcUtils.free(rs, ps, conn);
        }
    }

 4.利用结果集元数据将查询结果封装为map:

public List<Map<String, Object>> read(String sql) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int count = rsmd.getColumnCount();
            String[] colNames = new String[count];
            for (int i = 1; i <= count; i++) {
                // System.out.print(rsmd.getColumnClassName(i) + "\t");//类型
                // System.out.print(rsmd.getColumnName(i) + "\t");//数据表的字段
                // System.out.println(rsmd.getColumnLabel(i));//别名
                colNames[i - 1] = rsmd.getColumnLabel(i);
            }
            List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();

            while (rs.next()) {
                Map<String, Object> data = new HashMap<String, Object>();
                for (int i = 0; i < colNames.length; i++) {
                    data.put(colNames[i], rs.getObject(colNames[i]));
                }
                datas.add(data);
            }
            return datas;
        } finally {
            JdbcUtils.free(rs, ps, conn);
        }
    }

 

jdbc零散知识点

标签:style   blog   io   color   os   ar   for   sp   数据   

原文地址:http://www.cnblogs.com/dashen/p/4060579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!