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();
            }

  • 相关阅读:
    LALR(1)语法分析生成器--xbytes
    <<摩托车修理技术与禅>>读书笔记
    xscript脚本
    CentOS安装Erlang
    TCP中close和shutdown之间的区别
    Erlang高阶函数
    深度学习小记
    mac平台打造犀利的Android Studio开发环境
    MAC中如何配置两套android-sdk环境
    Ubuntu虚拟机编译Android6.0总结
  • 原文地址:https://www.cnblogs.com/TangGe520/p/8931261.html
Copyright © 2011-2022 走看看