Reputation: 51
I am searching for a Customer
by his Name
. The Name
is in TextField
.
Button Gets the Text in TextField And Checks in DB. The Button's Function is as follows:
Connection con=null;
PreparedStatement st =null;
ResultSet rs =null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/holt","root","");
st=con.prepareStatement("select * from customers where Name=?" );
String Customername = jTextField1.getText();
st.setString(1, Customername);
rs =st.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null,"Customer Found With ID = " + rs.getString("ID") );
StartSearchByName2 SB =new StartSearchByName2();
SB.setVisible(true);
close();
}
else{
JOptionPane.showMessageDialog(null,"Customer Does Not Exist");
}
}catch (Exception ex){
System.out.println("Error "+ex);
}
I want to get the Name
that the user entered in the StartSearchByName2
class.
I am trying to use this, but it returns me NULL
:
public class StartSearchByName2 extends javax.swing.JFrame {
/**
* Creates new form StartSearchByName2
*/
public StartSearchByName2() {
initComponents();
StartSByName SB = new StartSByName();
String cn = SB.jTextField1.getText();
JOptionPane.showMessageDialog(null,cn);
}
Thank you in advance.
Upvotes: 1
Views: 58
Reputation: 51
I was Able to Solve this problem by just using Static String for customer Name. Then Later when i call this string in second class, i will get the last updated value of this string.
Upvotes: -1
Reputation: 285405
Yep, my assumption was correct: you're getting your JTextField's text near when the JTextField has been created and not after the GUI has been displayed and the user has added information to it. If so, the key here is to extract the text from within some event such as an ActionListener. Give the GUI a JButton, say called submitButton, add an ActionListener to the JButton and in the listener get your text.
Upvotes: 2