zoukankan      html  css  js  c++  java
  • 如何使用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();
            }

  • 相关阅读:
    JavaWeb——库存管理系统(1)jsp部分---18.12.13
    Java——多线程---18.11.22
    Java——异常处理---18.11.14
    Java——final代码块是否一定被执行---18.11.08
    暑期的周总结们
    Javaweb——四则运算---18.11.01
    Java——equals方法---18.10.18
    微信小程序记账本进度六
    微信小程序记账本进度七
    微信小程序记账本进度一
  • 原文地址:https://www.cnblogs.com/TangGe520/p/8931261.html
Copyright © 2011-2022 走看看