• JDBC数据读写基本操作


    编写DAO数据处理组件

    定义添加数据方法

    定义更新数据方法

    定义删除数据方法

    定义各种业务查询数据方法

    package com.xzit.dao;
    
    import com.xzit.db.util.DataSourceManager;
    import com.xzit.pojo.Department;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    
    /*
    * 数据访问处理组件,实现对dep_tab 部门表进行增,删,改,查操作
    * */
    public class DepartmentDao {
    
        /*
        * 添加新部门的方法
        * @param dep 新添加的部门
        * */
        public int addDepartment(Department dep){
            int res = 0;
            String sql="insert into dep_table " +
                    "(id,name,createtime,descs) values (?,?,?,?)";  //sql插入语句
            Connection conn = DataSourceManager.getConnection();    //获取数据库连接
            PreparedStatement ps = null;    //获取PreparedStatement接口对象
    
            try {
                ps = conn.prepareStatement(sql);    //获取PreparedStatement对象
                /* 设置替换sql语句中的参数占位符? */
                ps.setString(1,"NO008");
                ps.setString(2,dep.getName());
                ps.setDate(3,new java.sql.Date(dep.getCreateDate().getTime()));
                ps.setString(4,dep.getDescs());
                res = ps.executeUpdate();
    
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {  //关闭数据库连接,释放资源
                DataSourceManager.close(ps);
                DataSourceManager.close(conn);
            }
            return res;
        }
    
        /**
         * 修改选定的部门对象
         * @param dep 欲修改的部门
         * @return
         */
        public int modifyDepartment(Department dep){
            int res = 0;
            String sql = "update dep_table set name = ?,createtime=?,descs=? "
                    + "where id=?";
            Connection conn = DataSourceManager.getConnection();    //获取数据库连接
            PreparedStatement ps = null;    //获取PreparedStatement接口对象
    
            try {
                ps = conn.prepareStatement(sql);
                ps.setString(1,dep.getName());
                ps.setDate(2,new java.sql.Date(dep.getCreateDate().getTime()));
                ps.setString(3,dep.getDescs());
                ps.setString(4,dep.getId());
                res = ps.executeUpdate();
            } catch (Exception ex) {
                ex.printStackTrace();
            }finally {
                DataSourceManager.close(ps);
                DataSourceManager.close(conn);
            }
            return res;
        }
    
        /**
         * 删除部门对象
         * @param id 删除部门的id
         * @return
         */
        public int deleteDepartmentById(String id){
            int res = 0;
            String sql = "delete from dep_table where id = '"+id+"'";
            Connection conn = DataSourceManager.getConnection();    //获取连接
            PreparedStatement ps = null;    //获取PreparedStatement接口对象
    
            try {
                ps = conn.prepareStatement(sql);
                //执行删除操作
                res = ps.executeUpdate();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {  //关闭数据库连接
                DataSourceManager.close(ps);
                DataSourceManager.close(conn);
            }
            return res;
        }
    }
    package com.xzit.test;
    
    import com.xzit.dao.DepartmentDao;
    import com.xzit.pojo.Department;
    
    import java.util.Date;
    
    public class TestInset {
        public static void main(String[] args) {
    
            Department dep = new Department();
            dep.setName("测试二部");
            dep.setCreateDate(new Date());
            dep.setDescs("测试部辅助部门");
    
            int res = new DepartmentDao().addDepartment(dep);
            System.out.println("影响数据库表的行数是 "+res);
        }
    }
    package com.xzit.db.util;
    
    import java.sql.*;
    
    /*
    * 数据源管理组件,提供最基本的通用的数据库连接
    * */
    public final class DataSourceManager {
    
        /*
        * 提供目标数据源的连接通用方法
        * */
        public static Connection getConnection(){
    
            Connection conn = null;
            /* 加载数据库驱动 */
            try {
                /* 加载数据库驱动 */
                Class.forName(Env.JDBC_DRIVER);
                conn = DriverManager.getConnection(Env.JDBC_URL,Env.JDBC_USER,Env.JDBC_PASSWORD);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return conn;
        }
    
        /*
        * 关闭数据库连接的通用方法
        * */
        public static void close(Connection conn){//关闭Connection
    
            try {
                if(conn != null && conn.isClosed()){
                    conn.close();   //关闭数据库连接
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    
        public static void close(Statement state){
            try {
                if (state != null && state.isClosed()){
                    state.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    
        public static void close(ResultSet set){
            try {
                if (set != null && !set.isClosed()){
                    set.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    package com.xzit.db.util;
    
    import java.util.ResourceBundle;
    
    public final class Env {
        /* 存储从属性文件读取的数据库属性配置信息 */
        public static final String JDBC_URL;
        public static final String JDBC_DRIVER;
        public static final String JDBC_USER;
        public static final String JDBC_PASSWORD;
        static {
            /* 获取配置文件的名称,使用getBundle()方法 */
            ResourceBundle resourceBundle = ResourceBundle.getBundle("dbconf");//不需要写后缀名
            /* 获取资源文件中的信息:使用getString()方法 */
            JDBC_URL=resourceBundle.getString("jdbc_url");
            JDBC_DRIVER=resourceBundle.getString("jdbc_driver");
            JDBC_USER=resourceBundle.getString("jdbc_user");
            JDBC_PASSWORD=resourceBundle.getString("jdbc_password");
        }
            public static void main(String[] args) {
            System.out.println(Env.JDBC_DRIVER);
            System.out.println(Env.JDBC_URL);
            System.out.println(Env.JDBC_USER);
    
        }
    }
    package com.xzit.pojo;
    
    import java.util.Date;
    
    /*
    * 映射dep_tab 部门信息表的实体类
    * */
    public class Department {
    
        private String id;
        private String name;
        private Date createDate;
        private String descs;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Date getCreateDate() {
            return createDate;
        }
    
        public void setCreateDate(Date createDate) {
            this.createDate = createDate;
        }
    
        public String getDescs() {
            return descs;
        }
    
        public void setDescs(String descs) {
            this.descs = descs;
        }
    }
    package com.xzit.test;
    
    import com.xzit.dao.DepartmentDao;
    
    public class TestDelete {
        public static void main(String[] args) {
            String id = "NO008";    //被删除部门对象的id(主键)
            int res = new DepartmentDao().deleteDepartmentById(id);
            System.out.println(res+" 条数据被删除");
        }
    }
  • 相关阅读:
    List 与 Array 的相互转化及 List、Array、Set转为 String
    Java 序列化介绍及 Redis 序列化方式
    SpringBoot 整合 redis 实现 token 验证
    SpringBoot 整合 Redis 使用
    Map 某 value 为 对象数组,转为 ArrayList 对象集合
    JWT 基本使用
    Spring session + redis 实现 session共享入门
    HttpServletRequest + Filter 添加 header
    Git ahead(超前) 又behind(落后)
    web应用中路径跳转问题-相对路径、绝对路径
  • 原文地址:https://www.cnblogs.com/zengyu1234/p/15435705.html
Copyright © 2020-2023  润新知