Search code examples
javasql-serverclassjtextfield

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.


Solution

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