我想将客户名称传递给班级StartSearchByName2
,我正在使用netbeans IDE
。客户名称来自Jtextfield
。
请给我一些解决方案。我有关于Java
的基本知识,我坚持了大约6个小时。
Class StartSByName
public class StartSByName extends javax.swing.JFrame {
String Customername;
public void close() {
WindowEvent winClosingEvent = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
}
String NameM(String Name){
Name = Customername;
System.out.println(Name);
return Name;
}
public StartSByName() {
initComponents();
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
jButtonAction5(evt);
}
public void jButtonAction5(ActionEvent evt) {
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=?");
Customername = jTextField1.getText();
st.setString(1, Customername);
rs = st.executeQuery();
jTextField1.setText(Customername);
if (rs.next()) {
JOptionPane.showMessageDialog(null, "Customer Found With ID = " + rs.getString("ID"));
jTextField1.setText(Customername);
String Name = null;
NameM(Name); // Calling NameM
StartSearchByName2 SB = new StartSearchByName2();
SB.setVisible(true);
close();
} else {
JOptionPane.showMessageDialog(null, "Customer Does Not Exist");
}
} catch (ClassNotFoundException | SQLException | HeadlessException ex) {
System.out.println("Error " + ex);
} }
类StartSearchByName2
public class StartSearchByName2 extends javax.swing.JFrame {
String Name;
StartSByName Sb= new StartSByName();
public StartSearchByName2() {
System.out.println(Sb.NameM(Name));
输出
Tabish Raza // This Output from StartSByName Class and when calling NameM
null // From StartSearchByName2, its twice null, but i called once and it is not giving me Correct String.
null //
答案 0 :(得分:1)
public class StartSearchByName2 extends javax.swing.JFrame {
String Name;
public StartSearchByName2(String pName) {
Name = pName;
System.out.println(Name);
}
在第一堂课中更改以下语句
StartSearchByName2 SB = new StartSearchByName2();
到
StartSearchByName2 SB = new StartSearchByName2(Name);