2012-10-07 29 views
0

可能重複:
retrieving values from database in java檢索值

我正在AGAIN另一個程序,檢索來自我創建的數據庫字段的輸入的數據/值。但是這一次,我輸入的值將來自我創建的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]); } 
在我的第二類

它就從我創建 這裏的數據庫字段的值輸入是程序代碼: 進口的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,經典,103)插入一些值。這裏是我的測試主類:

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()); 
} 
} 

當我運行它,它只是給我的房類空輸出,但我得到的GuestsInfo類,這是「埃裏卡」的輸出。你能幫助我嗎?我知道我昨天問這種問題,但我現在真的不知道這裏有什麼問題..

+0

我對'Room *類的SELECT * FROM guest WHERE guestID =?'好奇,不應該選擇從房間的桌子上?如果您在從結果集中檢索值時使用了列名,而不是使用列索引,那麼您應該選擇這種方法。有可能列索引可能會改變(對於名稱也可能是這樣),因爲順序取決於數據庫如何決定返回結果。 – MadProgrammer

+0

@MadProgrammer你能給我一個關於你在說什麼的例子嗎?實際上,我使用select語句從數據庫中檢索數據。請。糾正我。 – NOOBprogrammer

+0

@MadProgrammer說的是,如果你的SELECT語句是'SELECT fname,TABLE from lname',你應該使用rs.getString(「fname」)而不是rs.getString(1),因爲如果你把查詢改成'SELECT lname ,fname ...'rs.getString(1)你將不再檢索fname,但rs.getString(「fname」)會。 – jcern

回答

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每類。我要麼爲應用程序創建一個連接,要麼使用某種連接池

+0

謝謝先生。我甚至沒有注意到這一點。也許是。缺乏睡眠,也許因爲編程非常愚蠢。 XD – NOOBprogrammer

+0

缺乏睡眠。我是一個六個月大的快樂老爹,我有睡眠的記憶......:P – MadProgrammer