我有一個私人無效類,這是我的按鈕,該按鈕從數據庫中獲取一些resutls,我需要這些變量在另一個私人類中使用,我應該怎麼做? 抱歉,如果我錯了,我是一個新手,請幫助...訪問私有類變量Java
private void searchActionPerformed(java.awt.event.ActionEvent evt) {
String search_updt= search_username_fld.getText();
try{
Connection con = DriverManager.getConnection(host,dbname,dbpass);
Statement log = (Statement) con.createStatement();
//search for the user in database
ResultSet rs_log = log.executeQuery("SELECT * FROM Users WHERE Username='"+search_updt+"'");
while(rs_log.next()){
String rs_user = rs_log.getString("username");
String rs_pass = rs_log.getString("password");
String rs_first = rs_log.getString("first_name");
String rs_last = rs_log.getString("last_name");
//if authentication for user successful, go to user panel
if(rs_user.equals(search_updt)){
update.setVisible(false);
updating.setVisible(true);
this.setSize(430, 391);
}
//else show error
else {
JOptionPane.showMessageDialog(null, "Username Not Found!");
break;
}
}
}
catch(SQLException | HeadlessException e){
JOptionPane.showMessageDialog(null, e);
}
}
我需要訪問rs_user,rs_pass,...從另一個私有類
*吸氣劑*和* * setter方法是你的朋友。閱讀[封裝](http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29)。 – Maroun
「private void class」它們是什麼? – Batty
你應該按照這個教程 - http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html –