Java 如何在try/catch之外使用变量?
我有一个应用程序,其中用户输入用户名,应用程序从数据库查询返回用户ID 我遇到的问题是如何在Java 如何在try/catch之外使用变量?,java,try-catch,Java,Try Catch,我有一个应用程序,其中用户输入用户名,应用程序从数据库查询返回用户ID 我遇到的问题是如何在userIDLbl中显示userID 代码如下所示: JButton currentRoleBtn = new JButton("Get ID"); currentRoleBtn.setBounds(50, 50, 150, 30); currentRoleBtn.setToolTipText("Press to get the ID for the user"); current
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);
变量是其各自代码块的局部变量。如果要在try-catch外部使用它们,可以在块外部定义它们,然后在块内部使用它们,或者如注释所示,将更多代码移到try块内部
编辑:或者更好,如其他两个答案所述,将其设置为类成员。将其设置为类成员变量
class Foo
{
private int userID;
...
//getters setters
public void actionPerformed (ActionEvent e)
{
...
}
}
class User
{
private int userID;
//Constructors
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);
将userID声明为类级变量
class Foo
{
private int userID;
...
//getters setters
public void actionPerformed (ActionEvent e)
{
...
}
}
class User
{
private int userID;
//Constructors
public void actionPerformed (ActionEvent e)
{
}
}
因此,您可以在任何其他地方使用它。您必须将其设置为final,以便在try catch块之外访问它,但是您将无法更改此变量的值
class Foo
{
private int userID;
...
//getters setters
public void actionPerformed (ActionEvent e)
{
...
}
}
class User
{
private int userID;
//Constructors
public void actionPerformed (ActionEvent e)
{
}
}
通常,如果在
{}
范围内声明变量,则在该范围外无法访问该变量。对于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的使用是否恰当,或者错误是否得到了正确处理,这是一个单独的问题。)您有一个SQL注入漏洞。为什么不能将代码也移到内部?是什么阻止了它的工作?如果用户ID是结果集中的最后一个用户ID,也就是说,@subsvn将只有一个结果,那么您可以在类级别设置用户ID,并且可以在try/catch中或try-catch之后设置标签。