37.【專案案例】消防工程師培訓網站製作1--專案準備
阿新 • • 發佈:2018-12-15
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(); } } }