在java中从数据库中检索值

时间:2012-10-07 21:36:03

标签: java mysql debugging jtextfield

  

可能重复:
  retrieving values from database in java

我正在制作另一个程序,它从我创建的数据库中检索输入的字段数据/值。但这一次,我输入的值将来自我创建的JtextField。我想知道这里有什么问题因为当我运行它时输出总是为空。

在这个程序中,我将我的JTextField的输入值转换为int。这是:

public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == extendB)
{
    ExtensionForm extend = new ExtensionForm();
    extend.setVisible(true);
    }
else if(e.getSource()== searchB)            
{
//get text from the textField 
String guest = guestIDTF.getText();
//parse the string to integer for retrieving of data
int id = Integer.parseInt(guest);
GuestsInfo guestInfo = new GuestsInfo(id);
Room roomInfo = new Room(id);
String labels[] = {guestInfo.getFirstName()+" "+guestInfo.getLastName(),""+roomInfo.getRoomNo(),roomInfo.getRoomType(),guestInfo.getTime(),"11:00"}; for(int z = 0; z<labels.length; z++) { labelDisplay[z].setText(labels[z]); }

在我的第二个类中,它从我创建的数据库中检索输入的字段值 这是代码:     import java.sql。*;

公共教室 {

private String roomType;
private int  guestID, roomNo;

private Connection con;
private PreparedStatement statement;

public Room(){
    try {
         Class.forName("com.mysql.jdbc.Driver");
         con = DriverManager.getConnection(
                      "jdbc:mysql://localhost:3306/3moronsdb","root","");           
    } 
    catch (Exception e) {
         e.printStackTrace();
    }
}
public Room(int guestsID)
{
    this();
    try{
        statement = con.prepareStatement("SELECT * FROM guest WHERE guestID=?");
        statement.setInt(1, guestID);
        ResultSet rs = statement.executeQuery();
        while(rs.next()){
            this.guestID = rs.getInt(1);
            this.roomType = rs.getString(2);
            this.roomNo = rs.getInt(3);
        }
    }catch(Exception e){
        System.out.print(e);
    }
}
//Constructor for setting rate
public Room(String roomType, int roomNo)
{
    this();
    try
    {   
        statement = con.prepareStatement("Insert into room(roomType, roomNo) values(?,?)");
            statement.setString(1, roomType);
            statement.setInt(2, roomNo);
            statement.executeUpdate();
    }
    catch(Exception e)
    {
        e.printStackTrace();        
        return;
    }
}
//getting roomType
public String getRoomType(){
    return roomType;
}
//getting roomNo
public int getRoomNo(){
    return roomNo; 
}
//getting guestID
public int getGuestId(){
    return guestID;
}

}

我已经在我的3moronsdb中插入了一些值(1,Classic,103)。这是我的TEST主要课程:

public class TestMain {
    public static void main(String [] a){

        GuestsInfo guest = new GuestsInfo(1); //note that this instantiation is the other class which i just
 ask the other day.. (https://stackoverflow.com/questions/12762835/retrieving-values-from-database-in-java)   
        Room rum = new Room(1);
    System.out.print(rum.getRoomType()+"  "+ guest.getFirstName());
}
}

当我运行它时它只给我Room类的null输出,但我得到的是GuestsInfo类的输出,即'Ericka'。你能帮帮我们吗?我知道昨天我问这个问题,但我现在真的不知道这里有什么问题..

1 个答案:

答案 0 :(得分:2)

此方法中的select语句看起来不对。不应该从Room表??

中选择信息
public Room(int guestsID)
{
    this();
    try{
        // This line looks wrong, shouldn't this be selecting from the
        // room table??
        // statement = con.prepareStatement("SELECT * FROM room WHERE guestID=?");
        // instead???
        statement = con.prepareStatement("SELECT * FROM guest WHERE guestID=?");
        statement.setInt(1, guestID);
        ResultSet rs = statement.executeQuery();
        while(rs.next()){
            this.guestID = rs.getInt(1);
            this.roomType = rs.getString(2);
            this.roomNo = rs.getInt(3);
        }
    }catch(Exception e){
        System.out.print(e);
    }
}

您应该尝试使用列名,而不是使用列索引来检索值。虽然可能非常罕见,但数据库可能会以不同的顺序返回列,而不是您期望的代码。

(这可能由于多种原因而发生,数据库更新,数据库重新创建,数据库引擎改变它的想法......:P)

while(rs.next()){
    this.guestID = rs.getInt("guestID");
    this.roomType = rs.getString("roomType");
    this.roomNo = rs.getInt("roomNumber");
}

现在,显然,我对您的数据库结构一无所知,因此您需要适当更新列名。当你犯错并从错误的表中选择时,这会突出显示...

另外。您应该在不再需要时释放数据库资源

PreparedStatement statement = null;
try{
    statement = con.prepareStatement("SELECT * FROM guest WHERE guestID=?");
    statement.setInt(1, guestID);
    ResultSet rs = statement.executeQuery();
    while(rs.next()){
        this.guestID = rs.getInt(1);
        this.roomType = rs.getString(2);
        this.roomNo = rs.getInt(3);
    }
}catch(Exception e){
    System.out.print(e);
} finally {
    try {
        statement.close();
    }catch(Exception e){
    }
}

否则,您将面临数据库资源不足的风险:P

我也不会为每个班级创建一个Connection。我要么为应用程序创建一个连接,要么使用某种连接池