public class TestGai {
public static void main(String[] args) {
String drivername = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/mydbjdbc";
String username = "root";
String password = "123456";
Connection conn = null;
//插入sql
String sql = "update tb_stu set sage=? where sid=?";
PreparedStatement pstmt =null;
int result = 0;
try {
// 加载驱动
Class.forName(drivername);
// 建立连接
conn = DriverManager.getConnection(url, username, password);
//System.out.println(conn);
//创建PrepareStatement
pstmt = conn.prepareStatement(sql);
//设置sql语句的参数
pstmt.setInt(1, ?);
pstmt.setInt(2, ?);
/*
* 发送sql语句,并获得结果
* 增删改 executeUpdate()
* 查 executQuery()
*/
result = pstmt.executeUpdate();
//处理结果
if (result == 1) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
}
}