public class xiu {
private static int id;
public static void main(String[] args) {
Connection connection;
PreparedStatement stmt;
String URL = "jdbc:mysql://rm-2ze1h274pv1j5xbc58o.mysql.rds.aliyuncs.com:3306/cww";
String USER = "cww";
String PASSWORD = "grj5201314!";
String sql = "Update 测试1 Set id=?, username = ? where password = ?";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(URL, USER, PASSWORD);
stmt = connection.prepareStatement(sql);
stmt.setInt(1, 2);
stmt.setString(2,"chand");
stmt.setString(3, "464845");
stmt.executeUpdate();
stmt.close();
}catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
你这样写,很有可能是由于主键冲突导致的,一般修改是不建议修改主键的值的。可以用以下方法
String sql = "update 测试1 set username = ?,password = ? where id=?";
将主键ID作为修改行的条件值,然后进行需要修改的数据值。
你的数据库里面的值,我已经给你删了,哈哈哈~,不建议你使用中文的名字进行命名数据库的表明,这样可能会出现一些不可控的BUG,代码不规范,出错两行泪。
试着捕获Exception总异常类看看,或许某些异常没捕获-
你把数据库账号密码贴出来好吗。你这儿修改id看下id是不是主键。看看有没有主键冲突啥的