2013-05-17 86 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的使用是否适当,或错误是否得到妥善处理 - 这是一个单独的问题)。