Rohan21
Rohan21

Reputation: 353

Get value inputted in textfield from one Class to another

Here is my Index.java

String value1;
    btnNewButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                         value1=textField.getText();
                         String Cusname = null;


                         try{
                             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                             String connectionURL = "jdbc:sqlserver://ROHAN\\SQLEXPRESS;Database=sys;user=rohan;password=rurouni;";
                             Connection con = DriverManager.getConnection(connectionURL);
                                Statement st=con.createStatement();
                                ResultSet rs=st.executeQuery("select Name from loyaltycard where LCnum='"+value1+"'");
                                int count=0;
                                while(rs.next()){
                                    count++;
                                    Cusname = rs.getString("Name");

                                }
                               if(value1.equals("")) {
                               JOptionPane.showMessageDialog(null,"Enter Loyalty Card Number","Error",JOptionPane.ERROR_MESSAGE);
                               }
                               else if(count>0){

                              JOptionPane.showMessageDialog(null,"Login Successful \n"+Name,"Welcome",JOptionPane.PLAIN_MESSAGE);

                              new myitems().setVisible(true);
                                setVisible(false);
                               }
                               else{
                               textField.setText("");

                               JOptionPane.showMessageDialog(null,"Invalid Loyalty Card Number","Error",JOptionPane.ERROR_MESSAGE);
                               }}

                               catch(Exception e1){e1.printStackTrace();}
                         }


                });

            }

            public String getVal()
             {

                 return value1;
              }
        }

Here is my checkout.java

index LCval = new index();

     final String LCnum = LCval.getVal();

    JButton btnNewButton_1 = new JButton("Finish");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
             try{
                   int bal = 0;
                 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                 String connectionURL = "jdbc:sqlserver://ROHAN\\SQLEXPRESS;Database=sys;user=rohan;password=rurouni;";
                 Connection con = DriverManager.getConnection(connectionURL);
                PreparedStatement pst =null;
                ResultSet rs = null;
                    //Statement st=con.createStatement();

                    String sql="Select Balance  From loyaltycard where LCnum='"+LCnum+"' ";
              pst=con.prepareStatement(sql);
              rs=pst.executeQuery();

              if(rs.next()){
                  bal = rs.getInt("Balance");
                  JOptionPane.showMessageDialog(null,"Payment Successful!\n Your current balance is:"+bal,"Client",JOptionPane.INFORMATION_MESSAGE);
                    new index().setVisible(true);
                    setVisible(false);
              }
              }
              catch(Exception e1){e1.printStackTrace();}

                }});

i want to get the value inputted in the textfield of index.java to checkout.java when i press the button on checkout.java i get this error.

com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the varchar value 'null' to data type int.

Upvotes: 0

Views: 552

Answers (2)

Rohan21
Rohan21

Reputation: 353

i just added static in my public String getVal() and It just solved the problemns

Upvotes: 0

Szymon
Szymon

Reputation: 43023

SQL Server is returning NULL value which you don't handle in the code.

One way to do it could be to return zero instead of null in this situation:

String sql="Select isnull(Balance,0) as Balance  From loyaltycard where LCnum='"+LCnum+"' ";

BTW, do not concatenate your queries, this leaves you vulnerable to SQL injection. Use parameters instead.

UPDATE:

Based on the discussion, there's also more problems:

As the type of LCnum column is int, the where clause in the queries should be without quote characters:

"... where LCnum="+value1

Upvotes: 2

Related Questions