• 本季内容提要 
–ResultSet 简介 
–Java 数据类型与SQL数据类型的映射 
–ResultSet 常用方法 
• next() 
• getXXX() 
–实例 
• 从结果集中获得数据
##############Michael分割线################### 
• ResultSet 简介 
–用来获得SQL语句的查询结果 
–结果集包含了SQL语句的查询结果数据 
–调用语句的executeQuery方法返回结果集对象
• Java 数据类型与SQL数据类型的映射
image
• ResultSet 常用方法 
–next() 
• 返回boolean类型数据,用来判断结果集中是否有数据 
–getXXX() 
• 获得结果集中的数据项 
–可以根据列名称 
–可以根据列索引
• 实例 
–从结果集中获得数据
image
#####################Michael分割线########################
Main.java
package com.michael.main;    

import com.michael.jdbc.TestResultSet;    

public  class Main {    

         /**    
         * @param args    
         */
    
         public  static  void main(String[] args) {    
                 /**    
                ConnectionUtil cu = new ConnectionUtil();    
                //第一种方法    
                System.out.println("第一种方法:"+cu.getConnection());    
                //第二种方法    
                System.out.println("第二种方法:"+cu.getConnection("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin"));    
                //第三种方法    
                System.out.println("第三种方法:"+cu.openConnection());    
                */
    
                TestResultSet.query();    
        }    

TestResultSet.java
package com.michael.jdbc;    

import java.sql.Connection;    
import java.sql.ResultSet;    
import java.sql.SQLException;    
import java.sql.Statement;    

public  class TestResultSet {    
         public  static  void query(){    
                Connection conn =  new ConnectionUtil().openConnection();    
                 try {    
                        Statement stmt = conn.createStatement();    
                        String sql =  "select id,name,email from customertbl";    
                        ResultSet rs = stmt.executeQuery(sql);    
                         while(rs.next()){    
                                 int id = rs.getInt(1);    
                                String name = rs.getString( "name");    
                                String email = rs.getString( "email");    
                                System.out.println(id+ ":"+name+ ":"+email);    
                        }    
                }  catch (SQLException e) {    
                         // TODO Auto-generated catch block    
                        e.printStackTrace();    
                } finally{    
                         try {    
                                conn.close();    
                        }  catch (SQLException e) {    
                                conn =  null;    
                                e.printStackTrace();    
                        }    
                }    
        }    

image
#####################Michael分割线########################
在Servlet中我们要把后台查询结果显示在前台页面上,实现前后台的交互,我们可以把结果集封装到List中
Main.java
package com.michael.main;    

import com.michael.jdbc.TestResultSet;    

public  class Main {    

         /**    
         * @param args    
         */
    
         public  static  void main(String[] args) {    
                 /**    
                ConnectionUtil cu = new ConnectionUtil();    
                //第一种方法    
                System.out.println("第一种方法:"+cu.getConnection());    
                //第二种方法    
                System.out.println("第二种方法:"+cu.getConnection("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin"));    
                //第三种方法    
                System.out.println("第三种方法:"+cu.openConnection());    
                */
    
                TestResultSet.query();    
        }    

ConnectionUtil.java
package com.michael.jdbc;    

import java.sql.Connection;    
import java.sql.DriverManager;    
import java.util.Properties;    

public  class ConnectionUtil {    
         //第一种方法    
         public Connection getConnection(){    
                Connection conn =  null;    
                 try {    
                         //Class.forName加载驱动    
                        Class.forName( "com.mysql.jdbc.Driver");    
                         //DriverManager获得连接    
                        conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin");    
                        return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                return null;    
        }    
        //第二种方法    
        public Connection getConnection(String driver,String url,String user,String password){    
                Connection conn = null;    
                try {    
                        //Class.forName加载驱动    
                        Class.forName(driver);    
                        //DriverManager获得连接    
                        conn = DriverManager.getConnection(url,user,password);    
                        return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                return null;    
        }    
        //第三种方法    
        public Connection openConnection(){    
                String driver = "";    
                String url = "";    
                String user = "";    
                String password = "";    
                Properties prop = new Properties();    
                Connection conn = null;    
                try {    
                        //加载属性文件    
                        prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));    
                        driver = prop.getProperty("driver");    
                        url = prop.getProperty("url");    
                        user = prop.getProperty("user");    
                        password = prop.getProperty("password");    
                        //Class.forName加载驱动    
                        Class.forName(driver);    
                        //DriverManager获得连接    
                        conn = DriverManager.getConnection(url,user,password);    
                        return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                return null;    
        }    


TestResultSet.java
package com.michael.jdbc;    

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

public  class TestResultSet {    
         public  static List query(){    
                Connection conn =  new ConnectionUtil().openConnection();    
                 try {    
                        Statement stmt = conn.createStatement();    
                        String sql =  "select id,name,email from customertbl";    
                        ResultSet rs = stmt.executeQuery(sql);    
                         //将结果集封装到List中    
                        List list =  new ArrayList();    
                         while(rs.next()){    
                                 // 可以根据列名称也可以根据列索引    
                                 int id = rs.getInt(1);    
                                String name = rs.getString( "name");    
                                String email = rs.getString( "email");    
                                System.out.println(id+ ":"+name+ ":"+email);    
                                Customer c =  new Customer();    
                                c.setId(id);    
                                c.setName(name);    
                                c.setEmail(email);    
                                 //将对象存放到list容器中    
                                list.add(c);    
                        }    
                         return list;    
                }  catch (SQLException e) {    
                         // TODO Auto-generated catch block    
                        e.printStackTrace();    
                } finally{    
                         try {    
                                conn.close();    
                        }  catch (SQLException e) {    
                                conn =  null;    
                                e.printStackTrace();    
                        }    
                }    
                 return  null;    
        }    

Customer.java
package com.michael.jdbc;    
//对象关系映射ORM    
public  class Customer {    
         private  int id;    
         private String name;    
         private String email;    
         public String getEmail() {    
                 return email;    
        }    
         public  void setEmail(String email) {    
                 this.email = email;    
        }    
         public  int getId() {    
                 return id;    
        }    
         public  void setId( int id) {    
                 this.id = id;    
        }    
         public String getName() {    
                 return name;    
        }    
         public  void setName(String name) {    
                 this.name = name;    
        }    
}
image
#####################Michael分割线########################