文章目录
一、提出问题:更新表记录出现乱码
本次实训《GUI学生信息管理系统》,大部分同学用的是MySQL5.5(在配置MySQL过程中,字符编码设置为utf8)创建数据库也设置编码为utf8,在更新表操作中没有出现乱码,但是有个别同学安装的是MySQL5.7,结果在更新表操作中出现乱码。下面演示如何出现了乱码。
1、执行更新表操作之前的数据表
- 数据库student的表t_college
2、查看数据库连接管理类
3、学校数据访问接口实现类
package net.hw.student.dao.impl;
import net.hw.student.bean.College;
import net.hw.student.dao.CollegeDao;
import net.hw.student.dbutil.ConnectionManager;
import java.sql.*;
/**
* 功能:学校数据访问接口实现类
* 作者:华卫
* 日期:2020年7月11日
*/
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()) {
// 7. 创建学校实体对象
college = new College();
// 8. 利用当前记录各个字段值设置学校对象
college.setId(rs.getInt("id"));
college.setName(rs.getString("name"));
college.setPresident(rs.getString("president"));
college.setStartTime(rs.getTimestamp("start_time"));
college.setTelephone(rs.getString("telephone"));
college.setEmail(rs.getString("email"));
college.setAddress(rs.getString("address"));
college.setProfile(rs.getString("profile"));
}
// 9. 关闭结果集
rs.close();
// 10. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 11. 关闭数据库连接
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.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 {
// 7. 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
}
4、测试学校数据访问接口实现类
package net.hw.student.test;
import net.hw.student.bean.College;
import net.hw.student.dao.CollegeDao;
import net.hw.student.dao.impl.CollegeDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* 功能:测试学校数据访问接口实现类
* 作者:华卫
* 日期:2020年7月11日
*/
public class TestCollegeDaoImpl {
// 创建学校数据访问接口对象
private 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);
// 判断是否查询成功
if (college != null) {
// 输出学校信息
System.out.println("校名:" + college.getName());
System.out.println("校长:" + college.getPresident());
System.out.println("地址:" + college.getAddress());
System.out.println("邮箱:" + college.getEmail());
System.out.println("电话:" + college.getTelephone());
} else {
System.out.println("没有查询到学校记录!");
}
}
@Test
public void testUpdate() {
// 调用学校数据访问接口对象的查询方法
College college = dao.findById(1);
// 输出原校长
System.out.println("原校长:" + college.getPresident());
// 修改学校信息,改校长
college.setPresident("萌萌哒");
// 调用学校数据访问接口对象的更新方法
int count = dao.update(college);
// 判断是否更新成功
if (count > 0) {
System.out.println("恭喜,学校记录更新成功!");
System.out.println("新校长:" + dao.findById(1).getPresident());
} else {
System.out.println("遗憾,学校记录更新失败!");
}
}
}
- 运行测试方法testFindById(),查看结果
- 运行测试方法testUpdate(),查看结果
5、执行更新表操作之后的数据表
二、解决问题:让MySQL 5.7不出现乱码
由于要修改的MySQL 5.7的初始化配置文件my.ini
在目录C:\ProgramData\MySQL\MySQL Server 5.7
里,由于C:\ProgramData
是隐藏目录,需要设置一下才能显示出来。
1、显示隐藏项目
2、找到MySQL 5.7的初始化配置文件
- 在目录
C:\ProgramData\MySQL\MySQL Server 5.7
里找到my.ini
文件
3、修改MySQL 5.7的初始化配置文件
- 利用Notepad++打开初始化配置文件my.ini
- 找到
[mysql]
部分的# default-character-set=
(第66行)
- 去掉注释符
#
,在=
后添加utf8
- 找到
[mysqld]
部分的# character-set-server=
(第99行)
- 去掉注释符
#
,在=
后添加utf8
- 保存对文件my.ini的修改
4、重新启动MySQL57服务
- 打开
计算机管理
对话框
- 重启MySQL57服务
5、在NaviCat里运行数据库脚本恢复数据
6、运行测试方法testUpdate(),查看结果
7、在NaviCat查看更新操作之后的数据表
- 到此为止,MySQL 5.7的中文乱码问题得到完美解决。