错误代码示例:
@Test
public void test1() throws ClassNotFoundException, SQLException {
//1:注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2:获取链接
String url = "jdbc:mysql://localhost:3308/spj";
String user = "root";
String password = "数据库密码";
Connection conn = null;
Statement sta = null;
try {
conn = DriverManager.getConnection(url, user, password);
//3:定义sql语句
String sql = "update j set city='ss' where jno=1";
//4:获取执行sql语句的对象
sta = conn.createStatement();
//5:执行sql
int count = sta.executeUpdate(sql); //返回受影响的行数
//6:处理结果
System.out.println(count);
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(sta!=null)
//7:释放资源
sta.close();
if(conn!=null)
conn.close();
}
}
此时仅仅需要再 获取url连接时加上"?useUnicode=true&characterEncoding=utf-8&useSSL=false"即可;
@Test
public void test1() throws ClassNotFoundException, SQLException {
//1:注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2:获取链接
String url = "jdbc:mysql://localhost:3306/spj?useUnicode=true&characterEncoding=utf-8&useSSL=false";
String user = "root";
String password = "数据库密码";
Connection conn = null;
Statement sta = null;
try {
conn = DriverManager.getConnection(url, user, password);
//3:定义sql语句
String sql = "update j set city='ss' where jno=1";
//4:获取执行sql语句的对象
sta = conn.createStatement();
//5:执行sql
int count = sta.executeUpdate(sql); //返回受影响的行数
//6:处理结果
System.out.println(count);
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(sta!=null)
//7:释放资源
sta.close();
if(conn!=null)
conn.close();
}
}