当我尝试在Java中执行查询时遇到问题。当我尝试在SQL中将对象添加到我的数据库时,它给出了一个错误,指出该指令不发送结果集。当我在sql server中执行查询时,它工作正常并插入对象。请帮帮我。
public void addAlbum(String title, double price, String genre, String date, String home, String image) {
int number = getData().size();
boolean isThere = false;
Statement statement;
for(int i = 0; i < getData().size();i++){
if(getData().get(i).getTitle().equals(title)){
isThere = true;
}
}
if(!isThere){
try{
statement = connexion.createStatement();
String query = "use albums INSERT INTO dbo.Album(Album.artist_number, title, price, genre, date, home, image) VALUES(" + number + ", '" + title + "', " + price + ", '" + genre + "', '" + date + "', '" + home + "', '" + image + "')";
statement.executeQuery(query);
getData().add(new Album(String.valueOf(number),title, price, genre, date, home));
fireTableRowsInserted(getData().size() -1, getData().size() -1);
}catch(SQLException e){
JOptionPane.showMessageDialog(null,
"Error "
+ e.getMessage(), "Result",
JOptionPane.ERROR_MESSAGE);
}
}else{
JOptionPane.showMessageDialog(null,
"There is already an album with this name", "Result",
JOptionPane.ERROR_MESSAGE);
}
}
答案 0 :(得分:2)
您需要INSERT
使用SELECT
。 Statement.executeUpdate(String)
适用于PreparedStatement
。
{{1}}更好,更容易,更安全。 Statement.executeQuery(String)