ResultSet接口

来源:互联网 发布:2144传奇盛世翅膀数据 编辑:程序博客网 时间:2024/05/22 15:45

数据库的操作主要是查询和更新操作。查询操作会把数据库中查询结果返回给用户
使用sql中的select语句,可以将数据库中的全部结果查询出来,所有的查询记录将使用ResultSet进行接收,并使用ResultSet显示内容。
在操作的时候,使用Statement中的executeQuery()方法进行数据库查询。 此方法的返回值就是ResultSet接口。

import java.sql.Connection ;import java.sql.DriverManager ;import java.sql.SQLException ;import java.sql.Statement ;import java.sql.ResultSet ;import java.util.Date ;public class ResultSetDemo01{    //定义MySql数据库驱动程序    public static final String driver = "org.gjt.mm.mysql.Driver" ;    //定义Mysql数据库的连接地址    public static final String url = "jdbc:mysql://localhost:3306/spiderman" ;    //定义数据库名称    public static final String user = "root" ;    //定义数据库连接密码    public static final String pass = "" ;    public static void main(String[] args)    {        //加载数据库驱动程序        try        {            Class.forName(driver) ;             }        catch (ClassNotFoundException e)        {            e.printStackTrace() ;            return ;        }        // 加载数据库连接        Connection conn = null ;        try        {            conn = DriverManager.getConnection(url,user,pass) ;        }        catch (SQLException e)        {            e.printStackTrace() ;            return ;        }        //执行数据库操作        Statement sta = null ;        ResultSet rs = null ;   //保存查询到的结果        String sql = "select id,name,password,age,sex,birthday from mytable" ;//进行查询操作        try        {            sta = conn.createStatement() ; //实例化Statement对象            rs = sta.executeQuery(sql) ;    //执行数据库查询            while(rs.next())                //依次取出数据            {                int id = rs.getInt("Id") ;  //取出id列内容                String name = rs.getString("name") ;                String password = rs.getString("password") ;                int age = rs.getInt("age") ;                String sex = rs.getString("sex") ;                java.util.Date d = rs.getDate("birthday") ;                System.out.print("编号: "+id) ;                System.out.print("姓名: "+name) ;                System.out.print("年龄: "+age) ;                System.out.print("性别: "+sex) ;                System.out.println("生日: "+d) ;                System.out.println("密码: "+password) ;            }        }        catch (SQLException e)        {            e.printStackTrace() ;            return ;        }        finally        {            if(sta!=null)                   {                try                {                    sta.close() ;   //数据库操作 关闭                }                catch (SQLException e)                {                    e.printStackTrace() ;                }            }            if(conn!=null)                  {                try                {                    conn.close() ;//数据库连接 关闭                }                catch (SQLException e)                {                    e.printStackTrace() ;                }            }        }    }}

使用编号对应更方便

import java.sql.Connection ;import java.sql.DriverManager ;import java.sql.SQLException ;import java.sql.Statement ;import java.sql.ResultSet ;import java.util.Date ;public class ResultSetDemo01{    //定义MySql数据库驱动程序    public static final String driver = "org.gjt.mm.mysql.Driver" ;    //定义Mysql数据库的连接地址    public static final String url = "jdbc:mysql://localhost:3306/spiderman" ;    //定义数据库名称    public static final String user = "root" ;    //定义数据库连接密码    public static final String pass = "" ;    public static void main(String[] args)    {        //加载数据库驱动程序        try        {            Class.forName(driver) ;             }        catch (ClassNotFoundException e)        {            e.printStackTrace() ;            return ;        }        // 加载数据库连接        Connection conn = null ;        try        {            conn = DriverManager.getConnection(url,user,pass) ;        }        catch (SQLException e)        {            e.printStackTrace() ;            return ;        }        //执行数据库操作        Statement sta = null ;        ResultSet rs = null ;   //保存查询到的结果        String sql = "select id,name,password,age,sex,birthday from mytable" ;//进行查询操作        try        {            sta = conn.createStatement() ; //实例化Statement对象            rs = sta.executeQuery(sql) ;    //执行数据库查询            while(rs.next())                //依次取出数据            {                int id = rs.getInt(1) ; //取出id列内容                String name = rs.getString(2) ;                String password = rs.getString(3) ;                int age = rs.getInt(4) ;                String sex = rs.getString(5) ;                java.util.Date d = rs.getDate(6) ;                System.out.print("编号: "+id) ;                System.out.print("姓名: "+name) ;                System.out.print("年龄: "+age) ;                System.out.print("性别: "+sex) ;                System.out.println("生日: "+d) ;                System.out.println("密码: "+password) ;            }        }        catch (SQLException e)        {            e.printStackTrace() ;            return ;        }        finally        {            if(sta!=null)                   {                try                {                    sta.close() ;   //数据库操作 关闭                }                catch (SQLException e)                {                    e.printStackTrace() ;                }            }            if(conn!=null)                  {                try                {                    conn.close() ;//数据库连接 关闭                }                catch (SQLException e)                {                    e.printStackTrace() ;                }            }        }    }}
0 0