1、创建数据库表
1、t_college
2、t_status
3、t_student
4、t_user
撰写实体类
相同方式实现
2、Status类
3、Student类
4、User类
撰写接口Dao
接口实现类
package qingjiabo.student.dao.impl;
/**
- 功能:学校数据访问接口实现类
- 作者:卿加波
- 时间:
*/
import qingjiabo.student.dao.CollegeDao;
import qingjiabo.student.bean.College;
import qingjiabo.student.dao.CollegeDao;
import qingjiabo.student.dbutil.ConnectionManager;
import java.sql.*;
public class CollegeDaoImpl implements CollegeDao{
@Override
public College findById(int id) {
// 声明学校对象
College college = null;
// 1. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "select * from t_college where id = ?";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setInt(1, id);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 判断结果集是否有记录
if (rs.next()) {
// 实例化学校对象
college = new College();
// 利用当前记录字段值去设置学校对象的属性
college.setId(rs.getInt("id"));
college.setName(rs.getString("name"));
college.setPresident(rs.getString("president"));
college.setStartTime(rs.getDate("start_time"));
college.setTelephone(rs.getString("telephone"));
college.setEmail(rs.getString("email"));
college.setAddress(rs.getString("address"));
college.setProfile(rs.getString("profile"));
}
// 7. 关闭预备语句对象
pstmt.close();
// 8. 关闭结果集对象
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学校对象
return college;
}
@Override
public int update(College college) {
// 定义更新记录数
int count = 0;
// 1. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "update t_college set name = ?, president = ?, start_time = ?,"
+ " telephone = ?, email = ?,address = ?, profile = ? ,where id = ?";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, college.getName());
pstmt.setString(2, college.getPresident());
pstmt.setTimestamp(3, new Timestamp(college.getStartTime().getTime()));
pstmt.setString(4, college.getTelephone());
pstmt.setString(5, college.getEmail());
pstmt.setString(6, college.getProfile());
pstmt.setInt(8, college.getId());
// 5. 执行SQL,返回更新记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
}
数据库连接
package qingjiabo.student.dbutil;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
- 功能:数据库连接管理类
- 作者:卿加波
- 时间:2019/06/26
*/
public class ConnectionManager {
/**
* 数据库驱动程序
/
private static final String DRIVER = “com.mysql.jdbc.Driver”;
/*
* 数据库统一资源标识符
/
private static final String URL = “jdbc:mysql://localhost:3306/student”;
/*
* 数据库用户
/
private static final String USER = “root”;
/*
* 数据库密码
*/
private static final String PASSWORD = “1”;
/**
* 私有化构造方法,拒绝实例化
*/
private ConnectionManager() {
}
/**
* 获取数据库连接静态方法
*
* @return 数据库连接对象
*/
public static Connection getConnection() {
// 定义数据库连接
Connection conn = null;
try {
// 安装数据库驱动程序
Class.forName(DRIVER);
// 获取数据库连接
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 返回数据库连接
return conn;
}
/**
* 关闭数据连接静态方法
*
* @param conn
*/
public static void closeConnection(Connection conn) {
// 判断数据库连接是否非空
if (conn != null) {
try {
// 判断连接是否未关闭
if (!conn.isClosed()) {
// 关闭数据库连接
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 主方法:测试两个静态方法
*
* @param args
*/
public static void main(String[] args) {
// 获取数据库连接
Connection conn = getConnection();
// 判断数据库连接是否成功
if (conn != null) {
JOptionPane.showMessageDialog(null, "恭喜,数据库连接成功!");
} else {
JOptionPane.showMessageDialog(null, "遗憾,数据库连接失败!");
}
// 关闭数据库连接
closeConnection(conn);
}
}
测试学校数据访问接口实现类
package qingjiabo.student.text;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import qingjiabo.student.bean.College;
import qingjiabo.student.dao.CollegeDao;
import qingjiabo.student.dao.impl.CollegeDaoImpl;
/**
-
功能:测试学校数据访问接口实现类
-
作者:qingjiabo
*/
public class TextCollegeDaoImpl {//创建学校数据访问对象
CollegeDao dao = new CollegeDaoImpl();@Before
public void beforeTest(){
System.out.println(“温馨提示,单元测试开始了”);}
@After
public void afterTest(){
System.out.println(“温馨提示,单元测试结束了”);
}@Test
public void testFindByID(){
//创建学校数据访问对象的查找方法,得到学校对象
College college = dao.findById(1);
//输出学校对象
System.out.println(college);
}//更新学校对象
@Test
public void testUpdate(){
//调用数据访问对象的查找方法,得到学校对象
College college = dao.findById(1);
//修改学校内容,修改校长
college.setPresident(“王宏礼”);
//调用学校数据访问对象的更新方法
int count = dao.update(college);
//判断更新是否成功
if (count >0){
System.out.println(“学校记录更新成功!”);
System.out.println(“新校长”+dao.findById(1).getPresident());
}else {
System.out.println(“学校记录更新失败!”);
}
}
}