Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform action on OK of JOptionPane.showMessageDialog

I am working on my first desktop based Java project. I have 2 questions actually

1) How to perform action on OK button of JOptionPane.showMessageDialog.I want to navigate to a new Jframe say x.java on clicking ok.

2) I have a table named as user. This table has 8 columns userid (Primary Key), name, password,emailid, dob, mobileno ,city, date. Four column entries has to be fetched from a Jframe x and remaining four from other Jframe y.

I wrote the following code

For Frame X

PreparedStatement stm = con.prepareStatement("insert into user  

(userrid,name,password,emailid))values (?,?,?,?) ");

      stm.setString(1,id); // id is a public variable
      stm.setString(2,name);
      stm.setString(3,ps);
      stm.setString(4,email);
     stm.executeUpdate();

And for Frame Y. (userid is primary key)

public class Y extends javax.swing.JFrame
{  
    X o = new X(); // to access id variable from frame X

} 



PreparedStatement stm = con.prepareStatement(" update user set  dob ='? ', mobileno 
='?' ,city='?', date='?'  where userid= 'o.id' ");  

It keeps throwing exceptions for the above sql query

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

like image 312
Malwaregeek Avatar asked Jul 31 '13 19:07

Malwaregeek


2 Answers

1) How to perform action on OK button of JOptionPane.showMessageDialog.I want to navigate to a new Jframe say x.java on clicking ok.

int input = JOptionPane.showOptionDialog(null, "Hello World", "The title", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);

if(input == JOptionPane.OK_OPTION)
{
    // do something
}
like image 57
Eng.Fouad Avatar answered Sep 21 '22 18:09

Eng.Fouad


It keeps throwing exceptions for the above sql query

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

That's because you're using '?' in Update statement and ' is no needed. You should rewrite like this (assuming you're setting parameters properly):

PreparedStatement stm = con.prepareStatement("UPDATE user SET dob = ?, mobileno = ?, city = ?, date = ?  where userid= 'o.id' ");
like image 25
dic19 Avatar answered Sep 19 '22 18:09

dic19