我有一个私有的void类,这是我的按钮,该按钮正在从数据库获得一些结果,我需要在另一个私有类中使用这些变量,我应该怎么做? 对不起,如果我错了,我是新手请帮忙...
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,....答案 0 :(得分:0)
我认为你应该考虑使用Observer
模式,因为你有其他类需要的局部变量,而你的ResutSet
包含0或许多记录。
public class ResultSetEvent {
private String rs_user;
private String rs_pass;
// ...
public ResultSetEvent(String user, String pass) {
this.rs_user = user;
this.rs_pass = pass;
}
// getters / setters
}
public interface SearchActionResultListener {
void handleResult(ResultSetEvent event);
}
public class MyClass {
private List<SearchActionResultListener> listeners = new LinkedList<SearchActionResultListener>();
public void addSearchActionResultListener(SearchActionResultListener listener) {
listeners.add(listener);
}
public void removeSearchActionResultListener(SearchActionResultListener listener) {
listeners.remove(listener);
}
//...
private void searchActionPerformed(java.awt.event.ActionEvent evt) {
//...
while(rs_log.next()){
String rs_user = rs_log.getString("username");
String rs_pass = rs_log.getString("password");
//...
ResultSetEvent event = new ResultSetEvent(rs_user, rs_pass)
for(SearchActionResultListener l : this.listeners) {
l.handleResult(event);
}
}
//...
}
}
public class MyOtherClass implements SearchActionResultListener {
private MyClass reference;
public MyOtherClass() {
// reference = ...
reference.addSearchActionResultListener(this);
// ...
}
@Override
public void handleResult(ResultSetEvent event) {
// handle the event
}
}
答案 1 :(得分:-1)
@ user3315606:您的类结构不合适但是可以接受,因为您已经提到过您是java dev的新人。 我给你详细说明:
如果你想要将一个类的任何变量访问到另一个类中,那么它们应该来到具有私有访问说明符的类级别,并且它们应该具有关联的公共getter和setter。
根据您的要求,下面是Dummy课程:
public class Dummy{
private String rs_user;
private String rs_pass;
private String rs_first;
private String rs_last;
public void setRs_user(String rs_user){
this.rs_user = rs_user;
}
public void setRs_pass(String rs_pass){
this.rs_pass = rs_pass;
}
public void setRs_first(String rs_first){
this.rs_first = rs_first;
}
public void setRs_last(String rs_last){
this.rs_last = rs_last;
}
public String getRs_user{
return rs_user;
}
public String getRs_pass{
return rs_pass;
}
public String getRs_first{
return rs_first;
}
public String getRs_last{
return rs_last;
}
private void searchActionPerformed(java.awt.event.ActionEvent evt) {
...
setRs_user(rs_log.getString("username"));
setRs_pass(rs_log.getString("password"));
setRs_first(rs_log.getString("first_name"));
setRs_last(rs_log.getString("last_name"));
...
}
...
}
Now if you want to access rs_user, rs_pass ... in other classes then you can use getters method for these properties.
Like fot rs_user you can use getRs_user() method on the object of Dummy class.