Reputation: 387
I have a problem when I try to execute my query in Java. when I try to add an object to my database in SQL, it gives me a error saying that the instruction don't send the result set. When I execute the query in sql server, it work fine and it insert the object. Please help me.
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);
}
}
Upvotes: 2
Views: 2774
Reputation: 51030
You need to use Statement.executeUpdate(String)
for INSERT
. Statement.executeQuery(String)
is for SELECT
.
PreparedStatement
s are better, easier and safer. Using Prepared Statements.
Upvotes: 2