#Issue with CRUD operation DELETE in java

1 messages · Page 1 of 1 (latest)

static linden
#

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!

ocean rampartBOT
#

<@&987246584574140416> please have a look, thanks.

ocean rampartBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

unkempt aspen
#

you did ...=?

#

you have to give a value for the "?"

#

otherwise how can it knows what to select ?

static linden
#

yes but i dont want to specify the id

#

i want to delete any id that i select from application

unkempt aspen
#

wdym

static linden
#

wait let me show you

unkempt aspen
#

how can it knows what to select if you don't tell it that ?

static linden
#

when i click on this button i want the whole row to be deleted

#

but i have other rows too in my databse

#

database*

unkempt aspen
#

yes ?

static linden
#

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

unkempt aspen
#

then get the id of this row

#

and set the ? as this id

static linden
#

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

unkempt aspen
#

then what's the problem ?

#

get the id of the recipe you selected

#

and then do the request with it

static linden
#

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

unkempt aspen
#

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 ?

static linden
#

how do i write that in the code?

unkempt aspen
#

you probably coded the rows like

#

class Row {
int id;
...
}

#

then when you click on the row

#

then you create the request

#

and then you do

#

ps.setInt(1, id);

#

and then you execute the request

static linden
#

i havent coded the rows theyre made through database

unkempt aspen
#

wdym

#

what is the type of the rows ?

static linden
#

int

#

id is int

unkempt aspen
#

?

#

what's the problem then ?

static linden
#

this is the table

#

in my database

unkempt aspen
#

yes

static linden
#

because the query is not correct

#

what should i write in query?

unkempt aspen
#

yes it isn't

#

you are supposed to give a value

static linden
#

i understand that

#

what value?

#

if i wanna delete any row

unkempt aspen
#

the value that will replace "?"

unkempt aspen
static linden
#

every row has different id

static linden
unkempt aspen
#

which row ?

static linden
#

every row has different ids

static linden
#

in app

unkempt aspen
#

what does selecting give you ?

static linden
#

i mean pressing the button

#

when you click "delete" on some recipe it should delete it

#

from database

unkempt aspen
#

what is the type used to represent the rows ?

static linden
#

what type*

#

?

unkempt aspen
#

how do you represent the rows in your app ?

#

did you create a row class ?

static linden
#

nope

#

i made thru gui app

#

obriši means delete

unkempt aspen
static linden
#

and basically that what you see is what is in database

static linden
unkempt aspen
static linden
#

window builder or idk

unkempt aspen
#

which type

#

how do you represent the row in the gui

static linden
#

oh

#

text field

unkempt aspen
#

and for the id ?

#

same ?

static linden
#

and text area for larger ones

static linden
#

in app

unkempt aspen
#

wdym

static linden
#

only name, ingredients and preparation of recipe

#

i didnt put id to be shown of recipe in the app

unkempt aspen
#

even if you don't show the id

#

you still have to store it somewhere

static linden
#

in database

#

its integer

unkempt aspen
#

no

#

in your app

static linden
#

wait

unkempt aspen
#

you need to store the id of each row you display in your app

static linden
#

well i havent done that?

unkempt aspen
#

yes

#

so do it

static linden
#

so how do i do that?

unkempt aspen
#

create a variable

static linden
#

and when i create it what after it?

unkempt aspen
#

when you display the row

#

when you set each text field

#

you also set the variable to the id

static linden
#

do i really have to display id?

unkempt aspen
#

no

static linden
#

alright

#

thanks for help anyway

static linden
#

hi sorry to bother you but i did what you said but it doesnt work

#

when i click on delete nothing happens

spice spire
#

ull have to share ur code again. otherwise it's impossible for anyone to help u

static linden
#

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);
             }
            }
        }
    });
ocean rampartBOT
# static linden JButton btnNewButton_2 = new JButton("Obriši"); btnNewButton_2.addAction...

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);
      }
    }
  }
}
);
static linden
#

when i click on button "delete" absolutely nothing happens

#

it doesnt even give me the error

sweet dust
#

check in your db that it gets erased, if there is no error, then something has happend

static linden
#

it doesnt get erased

sweet dust
#

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

static linden
#

it doesnt work

spice spire
#

u can learn how to post it properly urself by reading the explanation by the bot when u started ur thread

spice spire