import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/*
* 与MYSQL连接后,修改表的操作
*/
public class UpdateDemo {
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement stmt = null;
try {
//第一步:加载MySQL的JDBC的驱动
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载驱动成功!");
//取得连接的url,能访问MySQL数据库的用户名,密码;数据库名 :test
String url = "jdbc:mysql://localhost:3306/test?"
+ "user=root&password=root&useUnicode=true&characterEncoding=UTF8";
String username = "root";
String password = "root";
//第二步:创建与MySQL数据库的连接类的实例
conn = DriverManager.getConnection(url, username, password);
// 创造SQL语句
String sql = "UPDATE student SET no = '456' WHERE no = '2012001'";
// 执行SQL语句
stmt = conn.createStatement();
stmt.executeUpdate(sql);
System.out.println("更新数据成功");
} catch (Exception e) {
e.printStackTrace();
stmt.close();
conn.close();
}
}
} |
|