1. 项目介绍
项目包括首页,列表页,内容页以及后台管理四个部分。
2. 项目准备
2.1 数据库
2.2 jar包
2.3 html页面
3.项目创建
3.1 创建web项目
创建web项目 EduPro
3.2 导入jar包
3.3 配置C3P0数据连接池
在src根目录下,创建c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/education</property>
<property name="user">root</property>
<property name="password">root</property>
</default-config>
</c3p0-config>
3.4 创建JavaBean
创建包cn.lctvu.bean包,创建News类
public class News {
private int id;
private String title;
private String content;
private String item;
private Date tdate;
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) {
this.content = content;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public Date getTdate() {
return tdate;
}
public void setTdate(Date tdate) {
this.tdate = tdate;
}
}
3.5 创建工具类
创建包cn.lctvu.utils包,在包中创建数据库连接工具类DataSourceUtils
package cn.itcast.itcaststore.utils;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 数据源工具
*/
public class DataSourceUtils {
private static DataSource dataSource = new ComboPooledDataSource();
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
public static DataSource getDataSource() {
return dataSource;
}
/**
* 当DBUtils需要手动控制事务时,调用该方法获得一个连接
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
Connection con = tl.get();
if (con == null) {
con = dataSource.getConnection();
tl.set(con);
}
return con;
}
/**
* 开启事务
* @throws SQLException
*/
public static void startTransaction() throws SQLException {
Connection con = getConnection();
if (con != null)
con.setAutoCommit(false);
}
/**
* 从ThreadLocal中释放并且关闭Connection,并结束事务
* @throws SQLException
*/
public static void releaseAndCloseConnection() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.commit();
tl.remove();
con.close();
}
}
/**
* 事务回滚
* @throws SQLException
*/
public static void rollback() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.rollback();
}
}
}