当前位置:编程学习 > JAVA >>

求高手指教,自己写的程序不能实现功能,不知道哪错了

这是一个数据库查询的程序:

public class QPhone extends JFrame{

/**
 * 
 */
private static final long serialVersionUID = 1L;

public static JTextField input1 = new JTextField();
public static JTextField input2 = new JTextField();
private static JTextArea infoshow = new JTextArea();
private Label l1 = new Label("科室代码:");
private Label l2 = new Label("电话号码:");
static Connection con;
static Statement st;
static InputStreamReader isr;
static BufferedReader br;
static String dm = null;
static String hm = null;
static String n1;
static String t1;
static String n2;
static String t2;

public QPhone(){
this.setTitle("电话查询");
this.setLayout(null);
this.setSize(500, 600);
this.setLocation(500, 100);

l1.setBounds(0, 0, 70, 25);
this.add(l1);
l2.setBounds(0, 30, 70, 25);
this.add(l2);
input1.setBounds(75, 0, 350, 25);
input1.addKeyListener(new KeyAdapter() {
     public void keyTyped(KeyEvent evt) {
      try {
jTextField1KeyTyped(evt);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
     }
    });
this.add(input1);
input2.setBounds(75, 30, 350, 25);
input2.addKeyListener(new KeyAdapter() {
     public void keyTyped(KeyEvent evt) {
      try {
jTextField2KeyTyped(evt);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
     }
    });
this.add(input2);

infoshow.setEditable(false);
JScrollPane jsp = new JScrollPane(infoshow);
jsp.setBounds(75, 60, 350, 380);
this.add(jsp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

}

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub

new QPhone();
                dm = input1.getText();

hm = input2.getText();


try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(java.lang.ClassNotFoundException e){
System.out.println(e);
}

try{
con = DriverManager.getConnection("jdbc:odbc:phone", "", "");
st = con.createStatement();
if(dm != null && hm == null)
{
ResultSet rs = st.executeQuery("SELECT name,tel FROM [Sheet1$] WHERE DM = '"+dm+"'");
while(rs.next()){
   n1 = rs.getString(1);
   t1 = rs.getString(2);
   //infoshow.append(n1 + ":" + t1 + "\n");
   
  
}
}

if(dm == null && hm != null){
ResultSet rs = st.executeQuery("SELECT name,tel FROM [Sheet1$] WHERE TEL LIKE '%"+hm+"%'");
while(rs.next()){
n2 = rs.getString(1);
t2 = rs.getString(2);
//infoshow.append(n2 + ":" + t2 + "\n");


}
}
else{
ResultSet rs = st.executeQuery("SELECT * FROM [Sheet1$]");
ResultSetMetaData rsmd = rs.getMetaData();
        int numberOfColumns = rsmd.getColumnCount();
while (rs.next()) {
              for (int i = 1; i <= numberOfColumns; i++) {
                  if (i >= 1)
                  infoshow.append("\n");
                  String columnValue = rs.getString(i);
                  infoshow.append(columnValue);
              }
              System.out.println("");
           }

}
st.close();
        con.close();
           
}catch(Exception e){
e.printStackTrace();
}

}
private void jTextField1KeyTyped(KeyEvent evt) throws SQLException {
  if (evt.getKeyChar()==KeyEvent.VK_ENTER) {
  infoshow.append(n1 + ":" + t1 + "\n");
  }
 }
}
private void jTextField2KeyTyped(KeyEvent evt) throws SQLException {
  if (evt.getKeyChar()==KeyEvent.VK_ENTER) {
  infoshow.append(n2 + ":" + t2 + "\n");
}
  }
 }
}


为什么我在文本框中输入合法字符之后,按回车显示结果却是null,null?是dm和hm没取到值还是n1和t1没取到值?真心求教!! --------------------编程问答-------------------- 前辈们都在哪啊,支援支援小弟啊 --------------------编程问答-------------------- 前辈们都在哪啊,支援支援小弟啊 --------------------编程问答-------------------- 具体问题,具体分析嘛.. --------------------编程问答-------------------- 我就是想让JTextField中的输入值赋予变量,加上按键易做图,以实现数据库查询
补充:Java ,  Eclipse
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,