Jdbc工具类的打包(包含jar包分享)

把jdbc封装的所有代码打成独立jar包,以后直接引用jar。

点击领取jar包以及配置文件

(1)创建一个新的工程

(2)创建包结构

  1. 打包

工具包

核心的代码:Jdbc.jar

src配置文件 :db.properties

依赖其他的包

log4j  +  src/log4j.properties

dbcp  + src/db.properties

mysql驱动

(4)项目中使用jdbc的封装包

之前的新闻发布系统,单独提供一个工程出来。

系统的jar包

配置文件

(5)完成功能

先创建合理的包结构,然后按照三层的步骤写代码

发布新闻

建表

......

实体类

package com.njwbhz.newssys.entity;

import com.njwbhz.jdbc.util.DateUtil;

import java.util.Date;

public class News {
    //属性
    private int id;
    private String title;
    private String Content;
    private Date createtime;

    //无参构造器
    public News() {
    }

    //有参构造器
    public News(int id, String title, String content, Date createtime) {
        this.id = id;
        this.title = title;
        Content = content;
        this.createtime = createtime;
    }

    //get和set方法
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return Content;
    }

    public void setContent(String content) {
        Content = content;
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    @Override
    public String toString() {
        return "News{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", Content='" + Content + '\'' +
                ", createtime=" + DateUtil.dateToStr(createtime , "yyyy-MM-dd HH:mm:ss") +
                '}';
    }
}

Dao

package com.njwbhz.newssys.dao;

import com.njwbhz.newssys.entity.News;

import java.sql.SQLException;

public interface NewsDao {
    void add(News news) throws SQLException;
}

DaoImpl

package com.njwbhz.newssys.dao.impl;

import com.njwbhz.jdbc.template.JdbcTemplate;
import com.njwbhz.newssys.dao.NewsDao;
import com.njwbhz.newssys.entity.News;

import java.sql.SQLException;

public class NewDaoImpl implements NewsDao {
    @Override
    public void add(News news) throws SQLException {
        String sql = "insert into t_news (t_title,t_content,t_createtime) values (?,?,now())";
        JdbcTemplate.insert(sql , news.getTitle() , news.getContent());
    }
}

TestDao

package com.njwbhz.newssys.test;

import com.njwbhz.newssys.dao.NewsDao;
import com.njwbhz.newssys.dao.impl.NewsDaoImpl;
import com.njwbhz.newssys.entity.News;

import java.sql.SQLException;

public class TestNewsDao {
    public static void main(String[] args) {
        NewsDao newsDao = new NewsDaoImpl();
        News news = new News();
        news.setTitle("123");
        news.setContent("123");
        try {
            newsDao.add(news);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

...检查数据

Exception

package com.njwbhz.newssys.exception;

import com.njwbhz.newssys.entity.News;

public class NewsSysException extends RuntimeException {
    public NewsSysException (String msg) {
        super (msg);
    }
}

Service

package com.njwbhz.newssys.service;

import com.njwbhz.newssys.entity.News;
import com.njwbhz.newssys.exception.NewsSysException;

public interface NewsService {
    void publish (News news) throws NewsSysException;
}

ServiceImpl

package com.njwbhz.newssys.service.impl;

import com.njwbhz.jdbc.tx.Transaction;
import com.njwbhz.newssys.dao.NewsDao;
import com.njwbhz.newssys.dao.impl.NewsDaoImpl;
import com.njwbhz.newssys.entity.News;
import com.njwbhz.newssys.exception.NewsSysException;
import com.njwbhz.newssys.service.NewsService;

import java.sql.SQLException;

public class NewsServiceImpl implements NewsService {
    //service肯定会调用dao,和处理事务
    private NewsDao newsDao = new NewsDaoImpl();
    private Transaction tx = new Transaction();
    @Override
    public void publish(News news) throws NewsSysException {
        if (news.getTitle().contains("你妹") || news.getContent().contains("你妹")) {
            throw new NewsSysException("不能含有脏话");
        }
        try {
            tx.begin();
            newsDao.add(news);
            tx.commit();
        } catch (SQLException e) {
            try {
                tx.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }
}

TestService

package com.njwbhz.newssys.test;

import com.njwbhz.newssys.entity.News;
import com.njwbhz.newssys.service.NewsService;
import com.njwbhz.newssys.service.impl.NewsServiceImpl;

public class TestNewsService {
    public static void main(String[] args) {
        NewsService newsService = new NewsServiceImpl();
        News news = new News();
        news.setTitle("1234");
        news.setContent("1234");
        newsService.publish(news);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FairyKunKun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值