可用于获取关于 ResultSet 对象中列的类型和属性信息的对象:java
/** * ResultSetMetaData * 是描述 ResultSet的元数据对象,即从中能够获得结果集中有多少列,列名是什么 * 1.获得 ResultSetMetaData对象:调用 ResultSet的 getMetaData()方法 * 2.经常使用方法 * getColumnCount() * --Returns the number of columns in this ResultSet object. * getColumnLabel(int column) * --Gets the designated column's suggested title for use in * printouts and displays. */ @Test public void test11() { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; Map<String, Object> values = new HashMap<>(); String sql = "SELECT Sno stuNo, Sname stuName, Ssex stuSex, " + "Sage stuAge, Sdept stuDept, S_entrance entrance " + "FROM Student WHERE Sno=?"; try { connection = getConnection2(); preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, "201215122"); resultSet = preparedStatement.executeQuery(); //获得ResultSetMetaData对象 ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); // if(resultSet.next()){ for(int i=0; i < resultSetMetaData.getColumnCount(); ++i) { String columnLable = resultSetMetaData.getColumnLabel(i + 1); Object columnValue = resultSet.getObject(columnLable); values.put(columnLable, columnValue); } } for(Map.Entry<String, Object> entry : values.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); System.out.println(key + " : " + value); } } catch(Exception e) { e.printStackTrace(); }finally { if(resultSet != null) { try { resultSet.close(); } catch(Exception e) { e.printStackTrace(); } } if(preparedStatement != null) { try { preparedStatement.close(); } catch(Exception e) { e.printStackTrace(); } } if(connection != null) { try { connection .close(); } catch(Exception e) { e.printStackTrace(); } } } } //getConnection2()方法: public Connection getConnection2() throws Exception { //1.准备链接数据库的4个字符串 //1.1 建立Properties对象 Properties properties = new Properties(); //1.2 获取jdbc.properties对应的输入流 java.io.InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"); //1.3 加载文件 properties.load(in); //1.4 给字符串赋值 String driver = properties.getProperty("driver"); String jdbcUrl = properties.getProperty("jdbcUrl"); String user = properties.getProperty("user"); String password = properties.getProperty("password"); //2.加载数据库驱动程序(对应的Driver实现类中有注册驱动的静态代码块) Class.forName(driver); //3.经过DriverManager的getConnection方法获取数据库链接 return DriverManager.getConnection(jdbcUrl, user, password); } //jdbc.properties driver=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/jdbctest user=root password=12345
JDBC学习笔记:mysql
1. 获取数据库链接 http://my.oschina.net/daowuming/blog/704243sql
2. 经过Statement执行更新、查询操做 http://my.oschina.net/daowuming/blog/704384数据库
3. 使用PrepareStatement http://my.oschina.net/daowuming/blog/704432函数
4. 使用ResultSetMetaData 对象处理结果集元数据 ----当前----学习
5. 使用DatabaseMetaData获取数据库信息 http://my.oschina.net/daowuming/blog/704553this
6. BLOB http://my.oschina.net/daowuming/blog/704593.net
7. 处理事务与隔离级别 http://my.oschina.net/daowuming/blog/704611code
8. 批量处理 http://my.oschina.net/daowuming/blog/704641对象
9. 数据库链接池 http://my.oschina.net/daowuming/blog/704700
10. 调用函数与存储过程 http://my.oschina.net/daowuming/blog/704813