【发布时间】:2015-02-28 05:26:39
【问题描述】:
我对 Java 比较陌生,而且我是自学的。目前我正在尝试开发一个包含文本字段和按钮的框架。该按钮在我的个人数据库中运行 SQL 查询,我希望 WHERE 子句使用文本字段中的文本填充。但是我无法将 txtField.getText() 值传递给构建 SQL 语句的类。它不会抓取在该字段中输入的内容,而是只给我该字段的默认文本。
我希望有人可以查看我的代码(如下)并告诉我哪里出错了。当我在 SQL 类中创建一个新的 Frame1 对象时,我有一种感觉,但我无法理解这个问题。
框架类的代码:
public Frame1() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
stateCodeVar = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
lblStateCodeVar = new javax.swing.JLabel();
txtDestVar = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel2.setText("Enter the state abbreviation:");
stateCodeVar.setName("txtState"); // NOI18N
stateCodeVar.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
stateCodeVarFocusLost(evt);
}
});
jButton1.setText("Search");
jButton1.setName("btnSearch"); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
lblStateCodeVar.setText("jLabel1");
lblStateCodeVar.setName("lblStateCodeVar"); // NOI18N
txtDestVar.setText("jTextField1");
txtDestVar.setName("txtDest"); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(stateCodeVar, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(149, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txtDestVar, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblStateCodeVar))
.addGap(63, 63, 63))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(13, 13, 13)
.addComponent(lblStateCodeVar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(stateCodeVar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
.addComponent(txtDestVar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(103, 103, 103))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
sqlSearchByState sql1 = new sqlSearchByState();
try {
sql1.sqlSearch();
System.out.println("The query is finished.");
String code = getStateCode();
/* System.out.println(code); */
}
catch (SQLException e){
e.printStackTrace();
}
}
private void stateCodeVarFocusLost(java.awt.event.FocusEvent evt) {
txtDestVar.setText(stateCodeVar.getText());
}
public String getStateCode(){
String result = stateCodeVar.getText();
return result;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Frame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Frame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Frame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Frame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel2;
public javax.swing.JLabel lblStateCodeVar;
public javax.swing.JTextField stateCodeVar;
public javax.swing.JTextField txtDestVar;
// End of variables declaration
}
SQL类的代码:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author Eli Oklesh
*/
public class sqlSearchByState {
public void sqlSearch() throws SQLException {
dbCon1 dbc = new dbCon1();
Frame1 f = new Frame1();
f.getStateCode();
String stateCode = f.getStateCode();
Connection conn1;
Statement stmt2;
String query = "SELECT * FROM users WHERE userLocation = '" + stateCode + "'";
conn1 = dbc.makeConnection();
try{
stmt2 = conn1.createStatement();
ResultSet rs = stmt2.executeQuery(query);
if (rs.next()){
while (rs.next()){
int userID = rs.getInt("ID");
String userFirstName = rs.getString("userFirstName");
String userLastName= rs.getString("userLastName");
String userLocation = rs.getString("userLocation");
System.out.println(userID + "\t" + userFirstName + "\t" + userLastName + "\t" + userLocation);
}
System.out.println(query);
}
else {
System.out.println("No records were returned.");
System.out.println(query);
}
}
catch (SQLException e){
System.out.println("There was an error:" + "\t" + e);
}
finally {
if (conn1 != null) {conn1.close();}
}
}
}
帮助!提前致谢!
【问题讨论】: