jdbc零散知识点

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") + "	"
 8                         + rs.getObject("name") + "	"
 9                         + rs.getObject("birthday") + "	"
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") + "	"
19                         + rs.getObject("name") + "	"
20                         + rs.getObject("birthday") + "	"
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") + "	"
                        + rs.getString("name") + "	" + rs.getDate("birthday")
                        + "	" + 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) + "	");//类型
                // System.out.print(rsmd.getColumnName(i) + "	");//数据表的字段
                // 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);
        }
    }
原文地址:https://www.cnblogs.com/dashen/p/4060579.html