Jdbc --mysql 查

Demo05Jdbc

只能查询一行

package cn.Dong;

import java.sql.*;

/**
 *  删除stu    id=6
 */

public class Demo05Jdbc {
    public static void main(String[] args) throws ClassNotFoundException {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs=null;

        //1.注册驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.定义sql
            String sql = "select * from stu";
            //3.获取connection对象
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Mydatabase?useSSL=false&serverTimezone=UTC", "root", "");
            //4.获取执行sql的对象statement
            stmt = conn.createStatement();
            //5.执行sql
             rs = stmt.executeQuery(sql);
            //6.处理结果
            //6.1 让游标向下移动一行
            rs.next();
            //6.2获取数据
            int id=rs.getInt(1);
            String name = rs.getString("name");
            int age=rs.getInt("age");
            String gender=rs.getString("gender");

            System.out.println(id+"--"+name+"---"+age+"--"+gender);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //stmt.close();
            //7.释放资源
            //避免空指针异常
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
升级  -v.2.0
Demo06Jdbc
package cn.Dong;

import java.sql.*;

/**
 *  删除stu    id=6
 */

public class Demo06Jdbc {
    public static void main(String[] args) throws ClassNotFoundException {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs=null;

        //1.注册驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.定义sql
            String sql = "select * from stu";
            //3.获取connection对象
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Mydatabase?useSSL=false&serverTimezone=UTC", "root", "");
            //4.获取执行sql的对象statement
            stmt = conn.createStatement();
            //5.执行sql
             rs = stmt.executeQuery(sql);
            //6.处理结果
            //6.1 让游标向下移动一行
            while (rs.next()){
                //6.2获取数据
                int id=rs.getInt(1);
                String name = rs.getString("name");
                int age=rs.getInt("age");
                String gender=rs.getString("gender");

                System.out.println(id+"--"+name+"---"+age+"--"+gender);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //stmt.close();
            //7.释放资源
            //避免空指针异常
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
;