JDBC创建基类

目录

一、创建resource 跟文件

 二、创建BaseDao类。设置属性位静态属性

三、创建static代码块:

四、加载驱动,创建Connnection连接

五、创建PrepareStatement , 编写sql语句

六、释放资源(方法重载,关闭不同连接)

七、创建实例,测试是否报错

八、创建接口方法

九、创建测试类测试


一、创建resource 跟文件

 

 二、创建BaseDao类。设置属性位静态属性

public class BaseDao {
    private static String driver;
    private static String url;
    private static String user;
    private static String pwd;

三、创建static代码块:

创建实例化会直接加载,得到连接方式,用户名,密码

 static {
        Properties properties = new Properties();
        InputStream inputStream = BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    driver = properties.getProperty("mysqldriver");
        url = properties.getProperty("mysqlurl");
        user = properties.getProperty("mysqluser");
        pwd = properties.getProperty("mysqlpwd");
       System.out.println(driver);
        System.out.println(url);
        System.out.println(user);
        System.out.println(pwd);
}

四、加载驱动,创建Connnection连接

    public Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user, pwd);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

五、创建PrepareStatement , 编写sql语句

   /*将新增,修改,删除统一抽象到一个方法中
     * params... 用法:表示n个参数[0,+无穷]
     * */
    public int executeUpdate(String sqlStr, Object... params){
        Connection connection = this.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = sqlStr;
        int num = -1;
        try {
            preparedStatement = connection.prepareStatement(sql);
            if(null!=params){
                for(int i = 0; i < params.length;i++){
                    preparedStatement.setObject(i+1,params[i]);
                }
            }
            num = preparedStatement.executeUpdate();
        } catch (SQLException e){
            e.printStackTrace();
        } finally {
            this.close(preparedStatement,connection);
        }
        return num;
    }

六、释放资源(方法重载,关闭不同连接)

    public void close(PreparedStatement preparedStatement, Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void close(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

七、创建实例,测试是否报错

    public static void main(String[] args) {
        BaseDao baseDao = new BaseDao();
        Connection connection = baseDao.getConnection();
        System.out.println(connection);
       
    }

八、创建接口方法

/*
* 对数据库中Dog表的操作定义,比如新增宠物,
* 根据id删除宠物,
* 根据健康值删除宠物,
* 根据id修改宠物信息,
* 根据id查询宠物信息,
* 根据多条件查询宠物信息
* */
public interface DogDao {
    Integer saveDog(Dog dog);

    Integer updateDog(Dog dog);

    Integer delById(Integer id);
    Integer delByhealth(Integer health);
}

九、创建测试类测试

public class DogDaoImpl extends BaseDao implements DogDao{
    @Override
    public Integer saveDog(Dog dog) {

        String sql="insert into dog(name,health,love,strain,lytm) value(?,?,?,?,now())";
        int num = super.executeUpdate(sql, dog.getName(), dog.getHealth(), dog.getLove(), dog.getStrain());
        if (num>0){
            System.out.println("添加成功");
        }
        return  num;


    @Override
    public Integer updateDog(Dog dog) {
        String sql = "update dog set name=?, health=?, love=?,strain=? where id=?";
        int num = super.executeUpdate(sql, dog.getName(), dog.getHealth(), dog.getLove(), dog.getStrain(), dog.getId());
        if(num>0){
            System.out.println("修改成功");
        }
        return num;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值