将值从数据库设置到文本字段

时间:2013-12-14 04:26:24

标签: java

我有以下代码

        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        conn = DriverManager.getConnection("jdbc:odbc:BankSystem");
        select = "Select Name From MalaysiaWithdraw where AccountNo = ?;";
        stmtSelect = conn.createStatement();
        stmtUpdate = conn.createStatement();
        pstmtSelect = conn.prepareStatement(select);

        //get value from textField and pass it into prepareStatement
        String accNo = accNumber.getText();
        pstmtSelect.setString(1, accNo);
        rsSelect = pstmtSelect.executeQuery();

如何从数据库中获取Name并将其设置为textfield,假设textfield是nameText。

3 个答案:

答案 0 :(得分:0)

如何从数据库中获取Name并将其设置到文本字段中,假设textfield是nameText。这样使用ResultSet就像这样

String Databasename;
while(rsSelect.next())
{

Databasename=rsSelect.getString("Name");
}
// your code for setting in the textfield

Databasename将拥有数据库中的Name。现在使用此设置在文本字段中

答案 1 :(得分:0)

假设查询只有一个结果,这是一个名称(select =“Select Name ...”)。

 rsSelect = pstmtSelect.executeQuery();

 String name;
 while (rsSelect.next()){
     name = rsSelect.getString(1);  // or getString("Name");  Name being the column name
 }

 textFieed.setText(name);

答案 2 :(得分:0)

假设文本字段名称为“textFieldName”

Resultset rsSelect = pstmtSelect.executeQuery();

String name=null;

while(rs.next()) {
   name = rs.getString("Name");
}

if(name!=null){
   textFieldName.setText(name);
}