hi guys! i would really appreciate help. so as you can see in the picture im getting error "No value specified for parameter 1" when im trying to delete one row in database through button in java gui application. i think the problem is in query - but i am not sure. i have quite a lot of rows in my database (im working in mysql) and through button in java gui i want to delete any row that i want. when i specify the id for example "100006" in query it deletes the row, but i want to be able to delete any row that i select. thanks!
#Issue with CRUD operation DELETE in java
1 messages · Page 1 of 1 (latest)
<@&987246584574140416> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
you need to specify the value of the argument of the sql statement
you did ...=?
you have to give a value for the "?"
otherwise how can it knows what to select ?
yes but i dont want to specify the id
i want to delete any id that i select from application
wdym
wait let me show you
how can it knows what to select if you don't tell it that ?
when i click on this button i want the whole row to be deleted
but i have other rows too in my databse
database*
yes ?
when i click on it it gives me that error
this is my database
i dont want a specific row to be deleted like for example the 10006 one
i want to be able to delete row thats selected in application
i hope that makes more sense
that works yes but thats not what i want
i wanna be able to delete row of the selected recipe in application
i have quite a lot of recipes in my application
so whenever i click "delete" it should delete any recipe that i selected in app
not just specific one
then what's the problem ?
get the id of the recipe you selected
and then do the request with it
but i have to do it without changing the code
i shouldnt specify id in the code
i should be able to delete any row
wdym
you associate an id to each row
so when someone click on a row
you get the id back
and do the request with it
what do you not understand here ?
how do i write that in the code?
you probably coded the rows like
class Row {
int id;
...
}
then when you click on the row
you do int id = row.id
then you create the request
and then you do
ps.setInt(1, id);
and then you execute the request
i havent coded the rows theyre made through database
yes
it gives me that error parameter
because the query is not correct
what should i write in query?
the value that will replace "?"
all rows ?
every row has different id
nope
which row ?
every row has different ids
what does selecting give you ?
i mean pressing the button
when you click "delete" on some recipe it should delete it
from database
what is the type used to represent the rows ?
then what did you use ?
and basically that what you see is what is in database
gui
yew but which type ?
window builder or idk
and text area for larger ones
wdym
only name, ingredients and preparation of recipe
i didnt put id to be shown of recipe in the app
wait
you need to store the id of each row you display in your app
oh
well i havent done that?
so how do i do that?
create a variable
and when i create it what after it?
when you display the row
when you set each text field
you also set the variable to the id
do i really have to display id?
no
hi sorry to bother you but i did what you said but it doesnt work
when i click on delete nothing happens
ull have to share ur code again. otherwise it's impossible for anyone to help u
sure
JButton btnNewButton_2 = new JButton("Obriši");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int action=JOptionPane.showConfirmDialog(null, "Do you want to delete?", "Delete", JOptionPane.YES_NO_OPTION);
if (action==0) {
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://stnt.vri.hr/aaa?serverTimezone=UTC", "aaa", "11");
String upit3="DELETE FROM recept where sifra_recepta=?" ;
PreparedStatement ps=con.prepareStatement(upit3);
int sifra_recepta = 0;
ps.setInt(1, sifra_recepta);
int obrisano=ps.executeUpdate();
if (obrisano>0)
JOptionPane.showMessageDialog(null, "Obrisano");
}
catch (Exception e2)
{
System.out.println(e2);
}
}
}
});
Detected code, here are some useful tools:
Formatted code
JButton btnNewButton_2 = new JButton("Obriši");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int action = JOptionPane.showConfirmDialog(null , "Do you want to delete?", "Delete", JOptionPane.YES_NO_OPTION);
if (action == 0) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://stnt.vri.hr/aaa?serverTimezone=UTC", "aaa", "11");
String upit3 = "DELETE FROM recept where sifra_recepta=?";
PreparedStatement ps = con.prepareStatement(upit3);
int sifra_recepta = 0;
ps.setInt(1, sifra_recepta);
int obrisano = ps.executeUpdate();
if (obrisano > 0) JOptionPane.showMessageDialog(null , "Obrisano");
} catch (Exception e2) {
System.out.println(e2);
}
}
}
}
);
what does this tell me?
when i click on button "delete" absolutely nothing happens
it doesnt even give me the error
check in your db that it gets erased, if there is no error, then something has happend
it doesnt get erased
try to printout the actual sql query to see that it's correct, and try it in the db
just to rule out one side of the operation
it doesnt work
its for us to be able to read ur code. since u posted raw unformatted code. which is absolutely unreadable to any helper
u can learn how to post it properly urself by reading the explanation by the bot when u started ur thread
we can't help u like that. u have to work with us, give us details, Screenshots, document what ur doing and seeing,...