1、创建数据库以及表
数据库student中有4个表:t_student、t_user、t_college、t_status
(3) t_student
(4) t_status
2、创建Java项目
(1)创建Java项目
(2)输入项目名
(3)在项目里创建文件夹help、images、lib
(4) 在lib里添加连接MySQL数据库的jar包
作为库添加到项目里(Add as Library…)
(5) 在images里添加图片
(6) 在help里添加帮助文档
3、创建实体类(与数据表一一对应)
首先在src目录里创建net.hw.student.bean包:
然后依次创建四个实体类:College、Status、Student与User,分别对应t_college表、t_status表、t_student表与t_user表。
(1)College实体
其他三个实体和College一样
4、数据库连接管理类ConnectionManager
package net.lsj.student.dbutil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
提供获取数据库连接于关闭的静态方法
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 USERNAME = "root";
/**
* 数据库密码
*/
private static final String PASSWORD = "1";
/**
* 私有化构造方法,拒绝实例化
*/
private ConnectionManager(){
}
/**
* 获取数据库连接
* @return 数据库连接对象
*/
public static Connection getConnection(){
//定义数据库连接
Connection conn = null;
try {
try {
//安装数据库驱动程序
Class.forName(DRIVER);
//获取数据库连接
conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
//返回数据库连接
return conn;
}
/**
* 关闭数据库连接静态方法
*
*/
public static void closeConnection(Connection conn){
if (conn != null){
try {
//判断连接是否未关闭
if (!conn.isClosed()){
//关闭数据库连接
connn.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
}
/**
* 主方法:测试两个静态方法
* @param args
*/
public static void main(String[] args) {
//获取数据库连接、
ConnectionManager conn = getConnection();
//判断数据库连接是否成功
if (conn !=null){
JOptionane.showMessageDialog(null,"恭喜,数据库连接成功!");
}else {
JOptionane.showMessageDialog(null,"遗憾,数据库连接失败!")
}
//关闭数据库连接
closeConnection(conn);
}
}
5、运行ConnectionManager,运行结果如下:
6、数据访问接口
之前定义了四张表:College、Status、Student与User表,对这四张表的操作就在相应的数据访问接口里进行,
定义四个数据访问接口:CollegeDao、StatusDao、StudentDao与UserDao。注意,这些接口要放到dao子包里
(1)学校数据访问接口
package net.lsj.student.dao;
import net.lsj.student.bean.College;
/**
public interface CollegeDao {
College findById(int id);
int update(College college);
}
(2)状态数据访问接口
(3)学生数据访问接口
(4)用户数据访问接口
7、数据访问接口实现类
在dao子包里创建impl子包,然后在里面创建四个数据访问接口的实现类。
(1)学校数据访问接口实现类CollegeDaoImpl
package net.lsj.student.impl;
import net.lsj.student.bean.College;
import net.lsj.student.dao.CollegeDao;
import net.lsj.student.dbutil.ConnectionManager;
import java.sql.*;
/**
public class CollegeDaoImpl implements CollegeDao{
/**
* 按ID查找学校
* @param id
* @return 学校对象
*/
@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.setStartime(rs.getTimestamp("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 {
}
//返回学校对象
return null;
}
/**
* 更新学校信息
* @param college
* @return
*/
@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.getStartime().getTime()));
pstmt.setString(4, college.getTelephone());
pstmt.setString(5, college.getEmail());
pstmt.setString(6, college.getAddress());
pstmt.setString(7, 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;
}
}
在根包net.hw.student里创建test子包,在里面创建测试类TestCollegeDaoImpl: