【数据库】员工信息管理系统
创建数据库
- employee表要求对离职员工进行软删除,因此设置isDeleted字段数据类型为bit(1),设置该字段默认值为0。具体代码如下:
ALTER TABLE employee MODIFY COLUMN isDeleted BIT NOT NULL DEFAULT 0;
连接数据库
1. 配置c3p0-config.xml文件
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <named-config name="helloc3p0"> <!-- 获取连接的基本信息 --> <property name="user">root</property> <property name="password">root</property> <property name="jdbcUrl">jdbc:mysql:///company3</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <!-- 数据库连接池管理 --> <!-- 若连接数不足时,一次性向数据库服务器申请多少个连接 --> <property name="acquireIncrement">5</property> <!-- 初始化数据库连接池 --> <property name="initialPoolSize">5</property> <!-- 数据库连接池中的最小连接数 --> <property name="minPoolSize">5</property> <!-- 数据库连接池中的最大连接数 --> <property name="maxPoolSize">10</property> <!-- 数据库连接池可维护的 Statement 数量 --> <property name="maxStatements">20</property> <!-- 每个连接可使用的 Statement 数量 --> <property name="maxStatementsPerConnection">5</property> </named-config> </c3p0-config>
2. 创建DbUtil工具类
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DbUtil { private static DataSource cpds=new ComboPooledDataSource("helloc3p0"); /** * 使用c3p0数据库连接池连接数据库 * * @return */ public static Connection getConnection() { Connection connection=null; try { connection=cpds.getConnection(); System.out.println("数据库连接成功"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("数据库连接失败"); } return connection; } /** * 获取statement对象 * * @param connection * @param sql * @return * @throws Exception */ public static Statement getStatement() { Statement stmt=null; try { stmt=getConnection().createStatement(); }catch(SQLException e) { e.printStackTrace(); } return stmt; } /** * 获取预编译的命令发送 * * @param connection * @param sql * @return */ public static PreparedStatement getPreparedStatement(String sql) { PreparedStatement ps=null; try { ps=getConnection().prepareStatement(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ps; } /** * 释放资源 * * @param rs * @param stmt * @param connection */ public static void closeAll(ResultSet rs,Statement stmt) { if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(stmt!=null) { try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(getConnection()!=null) { try { getConnection().close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 封装DML操作 * * @param sql * @param params * @return */ public static boolean executeDML(String sql,Object...params) { Connection connection=null; PreparedStatement ps=null; try { connection=getConnection(); //取消事务自动提交 connection.setAutoCommit(false); ps=DbUtil.getPreparedStatement(sql); for(int i=0;i<params.length;i++) { ps.setObject(i+1, params[i]); } int count=ps.executeUpdate(); if(count==0) { //失败回滚 connection.rollback(); return false; }else { connection.commit(); return true; } } catch (Exception e) { // TODO Auto-generated catch block try { //失败回滚 connection.rollback(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } e.printStackTrace(); } return false; } /** * 释放对象资源 * * @param stmt * @throws Exception */ public static void close(Statement stmt) throws Exception{ stmt.close(); } /** * 关闭数据库连接 * * @param connection */ public void closeCon() throws Exception{ if(getConnection()!=null) { getConnection().close(); } } public static void main(String[] args) { try { getConnection(); }catch (Exception e){ e.printStackTrace(); System.out.println("数据库连接失败"); } } }
*注意:配置数据库连接池时要把xml文件放在resources文件下

数据库JDBC运用实践
浙公网安备 33010602011771号