把jdbc封装的所有代码打成独立jar包,以后直接引用jar。
(1)创建一个新的工程
(2)创建包结构
- 打包
工具包
核心的代码: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);
}
}