如何让我的 sql 更新方法起作用?
How do I get my sql update method to work?
我正在做一个方法,它应该更新我在本地数据库中的标签(卡)的位置,这个方法(UpdateWallet)接受用户的输入并根据用户的内容覆盖我数据库中的记录已插入。在此方法代码的末尾,如果它在数据库中找到输入的 'tag' 并成功更新其位置,那么它应该 return true 并打印一条消息说更新已成功。否则,return false 但是 我有 2 个问题 :
1-尽管我收到了之前的消息(已成功更新),但这些值并未更新。
2- 无论我输入什么值,我的方法都满意并声称它 'updates' 记录。即使我的数据库中不存在插入的标签!其中这种情况应该只是 return false.
我的更新方法:
public boolean UpdateWallet(wallet upWallet) throws SQLException {
Connection dbConnection = null;
Statement statement = null;
String update = " UPDATE wallets SET Location = '"+upWallet.getWalletLocation()+"' WHERE Tag = '"
+ upWallet.getWalletTag()+"';";
try {
dbConnection = getDBConnection();
statement = dbConnection.createStatement();
// execute SQL update
statement.executeUpdate(update);
System.out.println("It has been updated successfully");
} catch (SQLException e) {
System.out.println(e.getMessage());
return false;
} finally {
if (statement != null) {
statement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
return true;
}
控制器代码:
System.out.println("Please enter a tag to update its location");
String tag = input.next();
System.out.println("Please enter Location ");
String loc = input.next();
wallet w = new wallet(tag,loc);
WalletsDAO.UpdateWallet(w);
钱包class:
public class wallet {
private String Name;
private String Location;
private String Tag;
public wallet(String Name, String Location, String Tag) {
this.Name = Name;
this.Location = Location;
this.Tag = Tag;
}
public wallet(String Tag) {
this.Tag = Tag;
}
public wallet(String Location, String Tag) {
this.Name = Name;
this.Location = Location;
this.Tag = Tag;
}
public String getWalletName() {
return Name;
}
public void setWalletName(String Name) {
this.Name = Name;
}
public String getWalletLocation() {
return Location;
}
public void setWalletLocation(String Location) {
this.Location = Location;
}
public String getWalletTag() {
return Tag;
}
public void setWalletTag(String Tag) {
this.Tag = Tag;
}
@Override
public String toString() {
String p = (this.Name + ',' + this.Location + ',' + this.Tag);
return p;
}
}
有人可以帮我吗?
您可以查看 executeUpdate 的 javadoc:https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#executeUpdate(java.lang.String)
您应该使用 executeUpdate() 的 int 结果来了解 none 或某些值是否已更新,而不是异常(如果没有值更新则没有异常)
问题在于以下调用:
wallet w = new wallet(tag,loc);
应该是
wallet w = new wallet(loc,tag);
匹配构造函数public wallet(String Location, String Tag)
.
另外,根据您的代码,remove/comment 以下代码中的注释行(即 this.Name = Name
):
public wallet(String Location, String Tag) {
//this.Name = Name;
this.Location = Location;
this.Tag = Tag;
}
我正在做一个方法,它应该更新我在本地数据库中的标签(卡)的位置,这个方法(UpdateWallet)接受用户的输入并根据用户的内容覆盖我数据库中的记录已插入。在此方法代码的末尾,如果它在数据库中找到输入的 'tag' 并成功更新其位置,那么它应该 return true 并打印一条消息说更新已成功。否则,return false 但是 我有 2 个问题 :
1-尽管我收到了之前的消息(已成功更新),但这些值并未更新。
2- 无论我输入什么值,我的方法都满意并声称它 'updates' 记录。即使我的数据库中不存在插入的标签!其中这种情况应该只是 return false.
我的更新方法:
public boolean UpdateWallet(wallet upWallet) throws SQLException {
Connection dbConnection = null;
Statement statement = null;
String update = " UPDATE wallets SET Location = '"+upWallet.getWalletLocation()+"' WHERE Tag = '"
+ upWallet.getWalletTag()+"';";
try {
dbConnection = getDBConnection();
statement = dbConnection.createStatement();
// execute SQL update
statement.executeUpdate(update);
System.out.println("It has been updated successfully");
} catch (SQLException e) {
System.out.println(e.getMessage());
return false;
} finally {
if (statement != null) {
statement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
return true;
}
控制器代码:
System.out.println("Please enter a tag to update its location");
String tag = input.next();
System.out.println("Please enter Location ");
String loc = input.next();
wallet w = new wallet(tag,loc);
WalletsDAO.UpdateWallet(w);
钱包class:
public class wallet {
private String Name;
private String Location;
private String Tag;
public wallet(String Name, String Location, String Tag) {
this.Name = Name;
this.Location = Location;
this.Tag = Tag;
}
public wallet(String Tag) {
this.Tag = Tag;
}
public wallet(String Location, String Tag) {
this.Name = Name;
this.Location = Location;
this.Tag = Tag;
}
public String getWalletName() {
return Name;
}
public void setWalletName(String Name) {
this.Name = Name;
}
public String getWalletLocation() {
return Location;
}
public void setWalletLocation(String Location) {
this.Location = Location;
}
public String getWalletTag() {
return Tag;
}
public void setWalletTag(String Tag) {
this.Tag = Tag;
}
@Override
public String toString() {
String p = (this.Name + ',' + this.Location + ',' + this.Tag);
return p;
}
}
有人可以帮我吗?
您可以查看 executeUpdate 的 javadoc:https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#executeUpdate(java.lang.String)
您应该使用 executeUpdate() 的 int 结果来了解 none 或某些值是否已更新,而不是异常(如果没有值更新则没有异常)
问题在于以下调用:
wallet w = new wallet(tag,loc);
应该是
wallet w = new wallet(loc,tag);
匹配构造函数public wallet(String Location, String Tag)
.
另外,根据您的代码,remove/comment 以下代码中的注释行(即 this.Name = Name
):
public wallet(String Location, String Tag) {
//this.Name = Name;
this.Location = Location;
this.Tag = Tag;
}