这个问题肯定在范围之内,但是我有问题找出原因,因为我宣布了将信息存储在循环之外的变量
public Member searchMember (String email) {
int Sid = 0;
String name ="";
String adr = "";
String phone = "";
String email2 = "";
String pass = "";
boolean status =false;
Member a = new Member(name,adr,phone,email2,pass);
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://compsi:3306/mcrowley","mcrowley","700463874");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
System.out.println(email);
String query = "Select id,name,address,phoneNum,email,password,LoginStatus From member WHERE email Like '%"+email+"%' ";
ResultSet results = stmt.executeQuery(query);
System.out.println("Before while loop");
int count = 0;
while (results.next() || count == 0 ) {
if (count == 0)
results.absolute(1);
System.out.println("Start while loop");
Sid = results.getInt(1);
//System.out.println(Sid+" this is test in jdbc");
name = results.getString(2);
//System.out.println(name+" this is test in jdbc");
adr = results.getString("address");
phone = results.getString("phoneNum");
email2 = results.getString("email");
pass = results.getString("password");
int stat = results.getInt("loginStatus");
if (stat == 0) {
status = false;
}
else {
status = true;
}
count++;
if (!results.next()) {
a.setName(name);
a.setAddress(adr);
a.setPhoneNumber(phone);
a.setEmail(email2);
a.setPassword(pass);
a.setId(Sid);
a.setStatus(status);
return a;
}
}
System.out.println("After while loop");
a.setName(name);
a.setAddress(adr);
a.setPhoneNumber(phone);
a.setEmail(email2);
a.setPassword(pass);
a.setId(Sid);
a.setStatus(status);
//create object & print
stmt.close();
con.close();
return a;
}
catch (Exception e) {
e.printStackTrace();
}
return a;
}
答案 0 :(得分:0)
所以基本上听起来你需要一个关于the scope of variables的进修课程。
基本上,你不会带着你想知道如何解决的破解代码来到这里。但是你正在滚动,因为你认为它应该不才能工作。
以下是我记忆的方法:每个范围都可以访问自身声明的变量以及直接父级的范围。也就是说,父作用域无法读取和修改任何即将成为欢呼的声音。
所以基本上,你的问题的答案是你的直觉是不正确的。变量范围不会以您认为的方式运行。