Guest User

Untitled

a guest
Jul 25th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. My project runs and connect to database (access .mbd) in net beans but jar does not connect to database, keep saying invalid USER OR PASSWORD
  2. import java.awt.Container;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9. import javax.swing.ImageIcon;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JOptionPane;
  14. import javax.swing.JPasswordField;
  15. import javax.swing.JTextField;
  16.  
  17. public class Login extends JFrame implements ActionListener {
  18.  
  19. Container c = getContentPane();
  20. private JButton btnLogin, btnCancel;
  21. private JLabel lblUName, lblPasswd;
  22. private JTextField txtUName;
  23. private JPasswordField txtPasswd;
  24.  
  25. public Login() {
  26. super("Login ...");
  27. this.setSize(350, 200);
  28. this.setLayout(null);
  29. this.setResizable(false);
  30. this.setLocation((Settings.getScreenSize().width / 2) - 175, (Settings.getScreenSize().height / 2) - 150);
  31. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  32.  
  33. lblUName = new JLabel("Username");
  34. lblPasswd = new JLabel("Password");
  35. txtUName = new JTextField();
  36. txtPasswd = new JPasswordField();
  37. btnLogin = new JButton("Login");
  38. btnCancel = new JButton("Cancel");
  39. lblUName.setBounds(50, 40, 140, 25);
  40. txtUName.setBounds(150, 40, 130, 25);
  41. lblPasswd.setBounds(50, 80, 140, 25);
  42. txtPasswd.setBounds(150, 80, 130, 25);
  43. btnLogin.setBounds(50, 120, 100, 25);
  44. btnCancel.setBounds(180, 120, 100, 25);
  45. btnLogin.addActionListener(this);
  46. btnCancel.addActionListener(this);
  47. this.add(lblUName);
  48. this.add(lblPasswd);
  49. this.add(txtUName);
  50. this.add(txtPasswd);
  51. this.add(btnLogin);
  52. this.add(btnCancel);
  53. }//constructor closed
  54. public void actionPerformed(ActionEvent e) {
  55. if (e.getSource() == btnLogin) {
  56. try {
  57. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  58. Connection con = DriverManager.getConnection("jdbc:odbc:student");
  59. try {
  60. Statement st = con.createStatement();
  61. ResultSet rs = st.executeQuery("SELECT * FROM UAD WHERE Username='" + txtUName.getText() +
  62. "' and Password='" + txtPasswd.getText() + "'");
  63. if (rs.next()) {
  64. if (rs.getString(3).equals("Student")) {
  65. userMDI frm = new userMDI();
  66. frm.setVisible(true);
  67. } else {
  68. new frmAdminMDI().setVisible(true);
  69. }
  70. this.dispose();
  71. }else{
  72. JOptionPane.showMessageDialog(null,"Invalid username or password","Invalid",JOptionPane.ERROR_MESSAGE);
  73. }
  74. con.close();
  75.  
  76. } catch (Exception ex) {
  77. JOptionPane.showMessageDialog(null, "Invalid username or password", "Invalid", JOptionPane.ERROR_MESSAGE);
  78. txtUName.setText("");
  79. txtPasswd.setText("");
  80. }//inner try catch closed
  81. } catch (Exception x) {
  82. JOptionPane.showMessageDialog(null, "Unable to connect to the database", "Connection error", JOptionPane.ERROR_MESSAGE);
  83. }//outer try catch closed
  84. }//if closed
  85.  
  86. if (e.getSource() == btnCancel) {
  87. System.exit(0);
  88. }//if closed
  89. }//actionPerformed() closed
  90. public static void main(String args[]) {
  91. new Login().setVisible(true);
  92. }
  93. }//class closed
  94.  
  95. Connection con = DriverManager.getConnection("jdbc:odbc:student","username","password");
Add Comment
Please, Sign In to add comment