In a DbInterface class a function openDB() opens connection to a Oracle DB on a server. For security reasons the user has to enter their password in a JFrame textarea before the program can proceed with the connection. Now this Jframe has an action listener which waits for the user to enter the password and calls the OpenDBContinue() method.
Now the issue is: openDB() does not wait on Jframe IO to finish and assuming DB has been opened returns back control to the calling class (whoever called openDB()) and they go ahead and start quering the DB which obviously fails!
Now how do I make openDB() wait on Jframe IO to finish? Here is the code to give you an idea.
public void openDB(int inFileInx,String inRemoteDBURLFull) throws FileNotFoundException
{
if(this.password!=null)
try
{ openDBcontinue(inFileInx,inRemoteDBURLFull);
}
catch(Exception exp)
{ DpmLogger.dtlException("SPDBInterfaceException:OpenDB", exp);
}
else {
passwd = new JFrame();
passwd.setLocation(SpdMain.bTabbedPanel.getWidth()/2,SpdMain.bTabbedPanel.getHeight()/2);
passwd.setTitle("Enter Passwd for user "+username);
JPasswordField p =new JPasswordField(10);
p.addActionListener(this);
p.setActionCommand(inFileInx+","+inRemoteDBURLFull);
passwd.add(p);
passwd.setPreferredSize(new Dimension(300,50));
passwd.pack();
passwd.setVisible(true);
pass=new Thread(new Runnable()
{
public void run() {
DpmLogger.dtlTraceOut("The password thread has completed and has got password from the user",DpmLogger.TRACE_RARE,myId);
}
});
try {
pass.join();
} catch (InterruptedException e)
{
DpmLogger.dtlTraceOut("Password thread unable to join",DpmLogger.TRACE_RARE,myId);
}
DpmLogger.dtlTraceOut("Password thread now joined",DpmLogger.TRACE_RARE,myId);
}
}
public void actionPerformed(ActionEvent e)
{ JTextField p=(JTextField)e.getSource();
if(password==null)
password=p.getText();
passwd.setVisible(false);
String[] inVars=e.getActionCommand().split(",");
try
{ openDBcontinue(Integer.parseInt(inVars[0]),inVars[1]);
pass.start();
}
catch(Exception exp)
{ DpmLogger.dtlException("SPDBInterfaceException:OpenDB", exp);
}
}
As you can see I am trying to make the method wait on a 'pass' thread with join(). The action listener starts the pass thread when IO finishes. But it does not work. OpenDB() returns without waiting on 'pass' to run. Is this because the method is not inside a thread? Do I have to make this DBInterface class extend Thread class? I am confused!