如何使用JDBC查询指定的记录

//连接数据库

public class JdbcDao {
    private Connection conn=null;
    private String strSql=null;


    public JdbcDao() {
        
        String driver ="com.mysql.jdbc.Driver";
        try {
            Class.forName(driver);
            String url ="jdbc:mysql://localhost:3306/dev?characterEncoding=utf8";
            conn=DriverManager.getConnection(url,"root", "");
            System.out.println("连接mysql数据库成功");
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

//指定id查询学生所有信息操作
    public Student queryOne(int id) {
        Statement stmt=null; 
        ResultSet rs=null;
        Student stu=new Student();
        try {
            strSql=" select * from student where id= "+id;    /根据ID查询Sq语句
            stmt=conn.createStatement(rs.TYPE_SCROLL_INSENSITIVE,rs.CONCUR_READ_ONLY);
            //执行SQL语句
            rs=stmt.executeQuery(strSql);     //查询返回结果
            while(rs.next()) {
                stu.setId(id);
                stu.setName(rs.getString("name"));
                stu.setPhone(rs.getString("phone"));
                stu.setStuNo(rs.getString("stuNo"));
                stu.setBirthday(rs.getString("brithday"));
                System.out.println("查询成功"+strSql);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

猜你喜欢

转载自www.cnblogs.com/TangGe520/p/8931261.html