What i want that is, when ever i click login button in login form. I want to set userid into util class. If i m going into another Company Form, at the load of company form, i want to getValue() from this util class.
I want suggestion that how can i setValue() when i m in login form that is extended by javax.swing.JFrame and How can i getValue() from util class when i m in Company Form also extended by javax.swing.JFrame.
Here is my dbConnection Class: and Login Form
////////////////////////////////////////
dbConnection.class
////////////////////////////////////////
import java.sql.*;
class dbConnection {
public Connection Conn ;
public Statement st,ss;
public ResultSet rs;
public String strUserID;
Statement ss=null;
/** Creates a new instance of Connection */
public dbConnection() {
try{
Class.forName("org.postgresql.Driver");
Conn = DriverManager.getConnection("jdbc
ostgresql://199.194.195.110:5432/masterdb","postgres","postgres");
st = Conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE );
System.out.println("Connection Established.........");
}
catch(Exception e){
System.out.println("Connection mein panga hai bhai "+ e );
}
}
public ResultSet getConnection (String str) throws SQLException
{
ResultSet rs = st.executeQuery(str);
return rs;
}
public String setUserID(String str) throws SQLException
{ String cc;
strUserID = str;
cc = strUserID;
return cc;
}
}
///////////////////////////////////
HERE IS LOGIN CLASS
//////////////////////////////////
import javax.swing.*;
import java.awt.event.WindowEvent.*;
import java.sql.*;
public class Login extends javax.swing.JFrame {
public ResultSet rs ;
//dbConnection cnn = new dbConnection();
public String strUserID;
/** Creates new form Login */
public Login() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
txtUserID = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
txtPassword = new javax.swing.JPasswordField();
lblStatus = new javax.swing.JLabel();
login = new javax.swing.JButton();
exit = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();
getContentPane().setLayout(null);
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setTitle("Login");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jLabel1.setFont(new java.awt.Font("Default", 1, 12));
jLabel1.setText("User ID");
getContentPane().add(jLabel1);
jLabel1.setBounds(40, 50, 60, 15);
txtUserID.setNextFocusableComponent(txtPassword);
txtUserID.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
txtUserIDFocusLost(evt);
}
});
txtUserID.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
txtUserIDKeyPressed(evt);
}
});
getContentPane().add(txtUserID);
txtUserID.setBounds(120, 50, 120, 19);
jLabel2.setFont(new java.awt.Font("Default", 1, 12));
jLabel2.setText("Password");
getContentPane().add(jLabel2);
jLabel2.setBounds(40, 80, 70, 15);
txtPassword.setBackground(new java.awt.Color(255, 204, 204));
txtPassword.setNextFocusableComponent(login);
getContentPane().add(txtPassword);
txtPassword.setBounds(120, 80, 120, 19);
lblStatus.setForeground(new java.awt.Color(255, 0, 0));
getContentPane().add(lblStatus);
lblStatus.setBounds(90, 180, 180, 20);
login.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/blueHome.png")));
login.setMnemonic('g');
login.setText("Go");
login.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loginActionPerformed(evt);
}
});
getContentPane().add(login);
login.setBounds(60, 130, 90, 32);
exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/redstop.png")));
exit.setMnemonic('x');
exit.setText("Exit");
exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitActionPerformed(evt);
}
});
getContentPane().add(exit);
exit.setBounds(150, 130, 90, 32);
jLabel6.setFont(new java.awt.Font("Default", 1, 12));
jLabel6.setLabelFor(lblStatus);
jLabel6.setText("Status:");
getContentPane().add(jLabel6);
jLabel6.setBounds(32, 180, 50, 15);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-331)/2, (screenSize.height-242)/2, 331, 242);
}
try{ System.out.println("Going to Check User");
String sql = "Select userid, pwd from user_manager where userid = '" + txtUserID.getText() + "' and pwd = '" + txtPassword.getText() + "'";
lblStatus.setText("Going Now");
////////////////
dbConnection cnn = new dbConnection();
Statement st = cnn.Conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE );
rs = st.executeQuery(sql);
if (rs.next()==false)
{
lblStatus.setText("No record found");
}
else{
rs.last();
if (rs.getString(1) == null)
{
System.out.println("No defined user is found");
lblStatus.setText("Invalid User ID ");
}
else
{
cnn.strUserID = txtUserID.getText();
Company_Year_Selection cd = new Company_Year_Selection();
cd.show();
setVisible(false);
cd.txtUserId.setText(txtUserID.getText());
}
}
///////////////
}
catch(Exception e){}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new Login().show();
}
// Variables declaration - do not modify
private javax.swing.JButton exit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel lblStatus;
private javax.swing.JButton login;
private javax.swing.JPasswordField txtPassword;
public javax.swing.JTextField txtUserID;
// End of variables declaration
}
/////////////////////////////////////////////////