小囙
2018-05-19 16:15
这是创建学生的实现:
@Override public void create(Student student) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; String sql = "insert into student (name, age) values (?,?)"; try { connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, student.getName()); preparedStatement.setInt(2, student.getAge()); preparedStatement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtil.release(resultSet, preparedStatement, connection); } }
这是单元测试:
@Test public void create() { StudentDAO studentDAO = new StudentDAOImpl(); Student student = new Student(); student.setName("赵六"); student.setAge(26); studentDAO.create(student); }
将url替换成这个:后面的参数用来指定编码
url = jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF8
数据库创建时编码也设置成UTF-8,还有表字段编码都保持一致
轻松愉快之玩转SpringData
34087 学习 · 119 问题
相似问题
回答 1
回答 1