zoukankan      html  css  js  c++  java
  • JDBC 1 利用Statement对数据库进行增删改查

    准备工作

      1新建po类:User

       private int id;                  
        private String name;
        private String pwd;
            set,get方法省略

       

     2  新建UserDao类,存放增删改查的相关方法,并且定义链接数据库的相关性信息(链接地址,用户名,密码);

    复制代码
    public class UserDao {
        private static String jdbcDriver = "com.mysql.jdbc.Driver";
        private static String jdbcUrl="jdbc:mysql://localhost:3306/homework";
        private static String jdbcuser="root";
        private static String jdbcpwd="123";
        public boolean ChaRu(User user){  //插入数据
            boolean flag=true;
        
            return flag;
        }
        public boolean XiuGai(User user){ //修改数据
            boolean flag=true;
      
            return flag;
        }
        public boolean ShanChu(int id){ //删除数据
            boolean flag=true;
    
            return flag;
        }
        public List<User> ChanZhao(){  // 查找数据
            List<User> list= new ArrayList<User>();
                   return list;
        }
      
    }
    复制代码

          3 在数据库中创建表user,表中设置,id    int

                                                          name varchar(20)

                                                          pwd  varchar(20)

    1 插入数据

    在进行增删改查操作结束之后,一定要关闭conn,st,rs(仅查找时用到此对象)对象。

    复制代码
        public boolean ChaRu(User user){
            boolean flag=true;
            Connection conn= null;
            Statement st=null;
            String sql="insert into user(name,pwd) values('"+user.getName()+"','"+user.getPwd()+"')";    //插入数据的SQL语句  
            try {
                Class.forName(jdbcDriver);   // 加载驱动器
                conn=DriverManager.getConnection(jdbcUrl, jdbcuser, jdbcpwd);  // 驱动利用驱动地址,数据库用户名,密码创建连接
                st=conn.createStatement(); // 连接创建数据库操作对象,Statement是执行数据库的重要方法
                int i=st.executeUpdate(sql);  // Statement 对象利用executeUpdate()方法执行sql。
                if(i==0){ 
                    flag=false;   // 后面测试用到flag变量,前面执行sql语句,如果成功i不为0,true代表执行成功,false代表失败。
                }
                
            } catch (ClassNotFoundException e) {  //加载驱动器失败异常
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {  //创建链接失败异常
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(conn!=null){     // 最后关闭连接
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(st!=null){   //关闭Statement
                    try {
                        st.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            return flag;  // 返回flag
        }
    复制代码
    复制代码
    public static void main(String[] args) {
            User user=new User();    //初始化 User类 对象user可以调用set,get方法设置或者获得数据。
            UserDao userDao = new UserDao();  //初始化UserDao 类,可以调用增删改差的各种方法。
            user.setName("lbzz"); // 设置姓名
            user.setPwd("123");   // 设置密码
            boolean flag=userDao.ChaRu(user);  //执行插入操作,返回值赋给flag
            if(flag){  //根据flag判断执行是否成功。
                System.out.println("插入成功");
            }else{
                System.out.println("插入失败");
            }
    
        }
    复制代码

    2 修改数据

    复制代码
    public boolean XiuGai(User user){
            boolean flag=true;
            Connection conn= null;
            Statement st= null;
            String sql= "update user set pwd='"+user.getPwd()+"' where name='"+user.getName()+"'";
            System.out.println(sql);
            try {
                Class.forName(jdbcDriver);
                conn=DriverManager.getConnection(jdbcUrl, jdbcuser, jdbcpwd);
                st=conn.createStatement();
                int i=st.executeUpdate(sql);
                if(i==0){
                    flag=false;
                }
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(conn!=null){
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(st!=null){
                    try {
                        st.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                }
            }
            
            
            return flag;
        }
    复制代码

    测试

    复制代码
    public static void main(String[] args) {
            User user= new User();
            UserDao userDao= new UserDao();
            user.setName("lbzz");
            user.setPwd("lbzz");
            boolean flag=userDao.XiuGai(user);
            if(flag){
                System.out.println("修改成功");
            }else{
                System.out.println("修改失败");
            }
        }
    复制代码

    3 删除记录

    复制代码
    public boolean ShanChu(int id){
            boolean flag=true;
            Connection conn= null;
            Statement st= null;
            String sql="delete from user where id="+id;
            try {
                Class.forName(jdbcDriver);
                conn=DriverManager.getConnection(jdbcUrl, jdbcuser, jdbcpwd);
                st=conn.createStatement();
                int i=st.executeUpdate(sql);
                if(i==0){
                    flag=false;
                }
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(conn!=null){
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(st!=null){
                    try {
                        st.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            
            return flag;
        }
    复制代码

    测试

    复制代码
    public static void main(String[] args) {
        UserDao userDao = new UserDao();
        boolean flag= userDao.ShanChu(1);
        if(flag){
           System.out.println("删除成功");
        }else{
           System.out.println("删除失败");
        }
     }
    复制代码

    4 查找数据

    前面增删改,程序除了sql语句不同外,其余相差无几,查找与前三个程序略有不同。

    复制代码
    public List<User> ChanZhao(){    //  查询出来的数据不止一条,所以使用集合
            List<User> list= new ArrayList<User>();  // 对集合进行初始化
            Connection conn= null;
            Statement st=null;
            ResultSet rs=null;  // ResultSet 返回的是一个结果集
            String sql="select * from user";
            try {
                Class.forName(jdbcDriver);
                conn= DriverManager.getConnection(jdbcUrl, jdbcuser, jdbcpwd);
                st=conn.createStatement();
                rs=st.executeQuery(sql);                 // 查询时使用 executeQuery()方法
                  while(rs.next()){                        // 对结果集进行遍历
                    User user = new User();
                  user.setName(rs.getString("name"));    // 需要查询什么就要像这样设置什么
                    list.add(user);                        //添加到集合
                  }
                
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
         }finally{
                  关闭conn,st,rs 
        }
            return list;  //返回list
        }
    复制代码

    测试

    复制代码
     public static void main(String[] args) {
          UserDao userDao = new UserDao();
          List<User> list=userDao.ChanZhao();
          for(User user:list){
              System.out.println(user.getName());
          }
     }
    复制代码
  • 相关阅读:
    【Python-Django模型迁移】用户数据库模型的迁移(对其他数据库迁移同样适用)!!!
    【Python-Django定义用户模型类】Python-Django定义用户模型类详解!!!
    【OpenCV-ANN神经网络自动驾驶】树莓派OpenCV神经网络自动驾驶小车【源码+实物】
    【Python-Django】Jinja2模板引擎配置教程详解!!!!
    js实现事件委托
    前端里面如何进行搜索引擎优化(SEO)
    ajax的优缺点
    css中px,em,rem,rpx的区别
    margin与padding的bug
    css3动画:transition和animation
  • 原文地址:https://www.cnblogs.com/baobeiqi-e/p/9884853.html
Copyright © 2011-2022 走看看