求助java数据库的问题、、
代码下方, 修改不了数据。。不知道出啥问题,求大侠们帮助。。不罗嗦。。上代码。
3个类。。。
第一个方法。
public void editcust()
{
System.out.println("请输入旧的会员姓名:");
use.setOldname(input.next());
System.out.println("请输入新的会员姓名:");
use.setNewname(input.next());
System.out.println("—————————————————————————修改结果————————————————————————");
// if(ct.Updata(use))
// {
//
// System.out.print("恭喜你修改成功!!");
// System.out.print("\t\t旧的会员姓名:"+use.getOldname()+"\t\t"+use.getNewname());
// }
第2个。。
public boolean Updata(User use) throws SQLException {
String oldname=use.getOldname();
String newName=use.getNewname();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from user where CustName=?";
int id = -1;
try {
pstmt = con.prepareStatement(sql);
pstmt.setString(1, oldname);
rs = pstmt.executeQuery();
if (rs.next()) {
id = rs.getInt("id");
}
if (id != -1)
return updataById(id,newName);
} finally {
con.close();
}
return false;
}
private boolean updataById(int id,String newName) throws SQLException {
Connection con = null;
PreparedStatement pstmt = null;
String sql = "updata user set CustName=? where id=?";
try {
pstmt = con.prepareStatement(sql);
pstmt.setString(1, newName);
pstmt.setInt(2, id);
return pstmt.execute();
} finally {
con.close();
}
}
第三个。。
package llak;
public class User {
private int id;
private String CustName;
private String newname;
private String CustNo;
private String oldname;
public String getNewname() {
return newname;
}
public void setNewname(String newname) {
this.newname = newname;
}
public String getOldname() {
return oldname;
}
public void setOldname(String oldname) {
this.oldname = oldname;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCustName() {
return CustName;
}
public void setCustName(String custName) {
CustName = custName;
}
public String getCustNo() {
return CustNo;
}
public void setCustNo(String custNo) {
CustNo = custNo;
}
}