2013-05-17 84 views
1

我有一個應用程序,其中用戶正在輸入用戶名,並且應用程序正在從數據庫查詢中返回用戶標識。如何在try/catch之外使用變量?

我遇到的問題是如何在userIDLbl中顯示userID

代碼看起來像這樣:

JButton currentRoleBtn = new JButton("Get ID"); 
    currentRoleBtn.setBounds(50, 50, 150, 30); 
    currentRoleBtn.setToolTipText("Press to get the ID for the user"); 
    currentRoleBtn.addActionListener(new ActionListener() 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      int userID; 
      String userName = adUserNameTxt.getText().toString(); 
      StringBuffer getRolesQuery1 = new StringBuffer("select id from hib.person where name = '"); 
      getRolesQuery1.append(userName).append("'"); 
      try 
      { 
       ResultSet rs = stmt.executeQuery(getRolesQuery1.toString()); 
       try 
       { 
        while (rs.next()) 
        { 
         userID = rs.getInt(1); 
         System.out.println("The User ID is: " +userID); 

        } 
       } 
       finally 
       { 
        rs.close(); 
       } 
      } 

      catch (SQLException e1) 
      { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
     } 
    }); 

    //Create UserID label 
    JLabel userIDLbl = new JLabel("User ID is: " +userID); 
    userIDLbl.setFont(new Font("Georgia", Font.PLAIN, 14)); 
    userIDLbl.setForeground(new Color(50,50, 25)); 
    userIDLbl.setBounds(25, 200, 200, 30); 
+6

您有一個SQL注入漏洞。 – SLaks

+0

爲什麼你不能只在內部移動代碼? –

+0

是什麼讓它不能正常工作?看起來好像這樣可以,如果你確定用戶ID是ResultSet中的最後一個用戶ID,也就是說。 – SubSevn

回答

3

將userID聲明爲類級變量。

所以你可以使用任何其他地方,你必須讓它最終訪問它嘗試捕捉塊外但隨後你不會能夠改變這個變量的值。

class User 
{ 
    private int userID; 

//Constructors 
public void actionPerformed (ActionEvent e) 
     { 

    } 
}  
2

變量是局部的各自的代碼塊。如果你想在try-catch之外使用它們,可以在塊之外定義它們,然後在裏面使用它們,或者如註釋所示,將更多的代碼移動到try塊中。

編輯:或者更好的是,正如其他兩個答案所述,使其成爲班級成員。

1

使其成爲類成員變量。

class Foo 
{ 
    private int userID; 
... 
//getters setters 
public void actionPerformed (ActionEvent e) 
     { 
      ... 
    } 
}  

永遠不要這樣做

StringBuffer getRolesQuery1 = new StringBuffer("select id from hib.person where name = '"); 
      getRolesQuery1.append(userName).append("'"); 

你不能信任用戶輸入。你想要這樣的東西來幫助減輕SQL注入:

PreparedStatement statement = conn.prepareStatement("select id from hib.person where name = ?"); 
statement.setString(1,userName); 
+0

非常感謝你 – DarthOpto

1

在一般情況下,如果聲明一個{}範圍內的變量,該變量是不可訪問該範圍之外。對於C,C++,Java和其他許多語言來說都是如此。在Java中,如果您在範圍之外聲明變量,請僅在條件範圍(由於if或歸因於try/catch而導致「有條件」)中進行設置,然後嘗試在您之後引用該變量離開那個條件範圍,編譯器和驗證器會抱怨變量沒有被初始化。

SomeClass someVar; 
try { 
    someVar = someValue; 
    someOtherStuff; 
    ... 
} 
catch ... { 
    ... 
} 
someOtherVar = someVar; // This access IS NOT allowed, because "someVar" is not certain to be initialized if an exception occurs. 

所以,一般情況下,您的解決方案是:

SomeClass someVar = null; // Or some other appropriate default value 
try { 
    someVar = someValue; 
    someOtherStuff; 
    ... 
} 
catch ... { 
    ... 
} 
someOtherVar = someVar; // This access IS allowed, because someVar is initialized, at least to "null". 

(請注意,這並未提及你的try/catch的使用是否適當,或錯誤是否得到妥善處理 - 這是一個單獨的問題)。