public class Demo {
public static void main(String agrs[]) {
Connection con = null
PreparedStatement pstmt = null
String sql = "delete from user where username=?"
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver") //设置数据库连接的驱动
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433databaseName=数据库") //设置数据库连接的 URL,用户名,密码
pstmt = con.prepareStatement(sql)
pstmt.setString(1, "aaa") // 设置SQL语句中username的值
int count = pstmt.executeUpdate()
if (count >0) {
System.out.println(" *** 作成功")
} else {
System.out.println(" *** 作失败")
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
//删除用户public String delete(id){
try{
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
String dbURL = "jdbc:sqlserver://localhost:1433DatabaseName=denglu"
String userName = "han"String userPwd = "123" Class.forName(driverName)
dbConn = DriverManager.getConnection(dbURL, userName, userPwd)
stml=dbConn.createStatement();
stml.executeUpdate("delete from test where id='" + id +"'")
}catch(Exception e){
e.printStackTrace()
}
return "success"
}
//修改用户 *** 作
public String modify(id){
try{
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
String dbURL = "jdbc:sqlserver://localhost:1433DatabaseName=denglu"
String userName = "han"String userPwd = "123" Class.forName(driverName)
dbConn = DriverManager.getConnection(dbURL, userName, userPwd)
stml=dbConn.createStatement();
stml.executeUpdate("update test set 字段名1=新值1,字段名2=新值2,...,字段名n=新值n, where id='" + id +"'")
}catch(Exception e){
e.printStackTrace()
}
return "success"
}
主要是把id传到方法里就好了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)