TextField tf = new TextField(10);
TextArea ta = new TextArea(10,15);private class ConfirmButton implements ActionListener{
private static final int INFORMATION_MESSAGE = 0; public void actionPerformed(ActionEvent e) {
if(tf.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Please Enter The Question!","Warning",INFORMATION_MESSAGE);
return;
}
else
{
String str = connect.readsql();
ta.setText(str);
}
}
 }
rs = stmt.executeQuery("select * from dept");while(rs.next()){
      if(m.tf.getText().equals(rs.getInt("deptno"))){
      str = rs.getString("dname");
     }
}如何实现就是在TextField里输入数据,然后把对应的数据输入到TextArea中。
DEPTNO DNAME
10      A
20      B
30      C
例如就是在tf里输入10,然后就能把A输入到ta中~

解决方案 »

  1.   

    如何实现就是在TextField里输入数据,然后把对应的数据输入到TextArea中。实现上面的功能也可以不用和数据库交互,其实很简单,就是给TextField加个监听事件,当按回车或者失去鼠标的时候出发一个事件取得TextField当前的值,然后再set到TextArea当中,如果是要把TextField的值插入到数据库中也可以写在监听事件里面。
      

  2.   


    public String getDeptName(String deptNo) throws DaoException {
    Strng deptName = null;
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    final boolean isConnSupplied = (conn != null);
    try {
    con = isConnSupplied ? conn : resourceManager.getConnection();
    ps = con.prepareStatement("select deptname from dept where deptno = ?");
    ps.setString(1, deptNo");
    rs = ps.executeQuery();
    while (rs.next()) {
    deptName = rs.getString(1);
    }
    return deptName;
    } catch (Exception e) {
    logger.error("getDeptName(String deptNo) failed. ", e);
    return null;
    } finally {
    resourceManager.close(rs);
    resourceManager.close(ps);
    if (!isConnSupplied) {
    resourceManager.close(con);
    }
    }
    }这个是查询数据库的方法。其中protected ResourceManager resourceManager;是一个管理数据库连接和关闭的公共类,很好实现。不知道你想要的是不是我提供的这些。汗。
      

  3.   

    对对对,就是不知道第二个的那种情况,对应的值得读取。给Button加了个监听事件,单击以后连接数据库,然后把里面的值赋给一个字符串变量,就是不知道如何把取得的值分开,在tf中输入数据库里的某个数据,通过查询在ta中显示与tf中输入对应的数据
      

  4.   

    DEPTNO DNAME
    10 A
    20 B
    30 C
    例如就是在tf里输入10,然后就能把A输入到ta中~
    为例子,我就借用一下2楼的链接数据了
    //private Strng deptName = ""; //接受ta里面的显示值//写个方法给按钮加事件
    jButton1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    getDeptName(e);
    }
    //这里和数据库交互
    public String getDeptName(String deptNo) throws DaoException {
       String deptNo=tf.getText();//获取tf的输入值    Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        final boolean isConnSupplied = (conn != null);
        try {
            con = isConnSupplied ? conn : resourceManager.getConnection();
            ps = con.prepareStatement("select DNAME from dept where deptno = ?");
            ps.setString(1, deptNo");//假如deptNo输入是10用deptNo做条件查出DNAME
            rs = ps.executeQuery();
            while (rs.next()) {
                deptName = rs.getString(1);
            }
            return deptName;//返回对应的DNAME
        } catch (Exception e) {
            logger.error("getDeptName(String deptNo) failed. ", e);
            return null;
        } finally {
            resourceManager.close(rs);
            resourceManager.close(ps);
            if (!isConnSupplied) {
                resourceManager.close(con);
            }
        }
    }//最后把 deptName 值显示在ta中;
    ta.setText(deptName );
    就是上面几步,你整理一下,具体的你得修改一下,关键代码已经写出来了。
      

  5.   

    public class Connect{

    Main m = new Main();
    private String deptName;

    public String getDeptName(){
    String deptNo1 = m.tf.getText();

    ResultSet rs = null;
    Statement stmt = null;
    Connection conn = null;
    PreparedStatement ps = null;
    try{
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
    stmt = conn.createStatement();

    ps = conn.prepareStatement("select DNAME from dept where deptno = ?");
    ps.setString(1,deptNo1);
    rs = ps.executeQuery();
    //System.out.println(rs);
    while(rs.next()){
    deptName = rs.getString(1);
    }
    }catch(ClassNotFoundException e){
     e.printStackTrace();
    }catch(SQLException e){
    e.printStackTrace();
    }finally{
    try{
    if(rs!=null){
    rs.close();
    rs = null;
    }
    if(ps!=null){
    ps.close();
    ps = null;
    }
    if(conn!=null){
    conn.close();
    conn = null;
    }
    }catch(SQLException e){
    e.printStackTrace();
    }
    }
    return deptName;//返回对应的DANME;
    }
    }
    监听事件下
    String str = connect.getDeptName();
    ta.setText(str);哪里又错了,还有那个函数带deptNo参数,String deptNo = m.tf.getText();的话好像会出错