Step-by-Step Installing JCreator and Connecting this Java Software to MS Access Database with Java GUI Login Tutorial

This is my own version of how to connect Java to MS Access database with a Login Tutorial. Because for now I will make tutorials for Java because my Facebook friends wanted me to have a tutorial about this programming language. Software to be used: JCreator LE 4.50 or NetBeans but I preferred JCreator, JDK 7 Download here, and Microsoft Access software. 1. Now, after installing all of these requirements, you will see the User Interface of JCreator like the image below. jcreator 2. Next, Go to File Menu, Click New, Choose File and Click it. jcreator 3. Now you will see the file wizard. Go to Java Classes, Click Java Class, and click Next. jcreator 4. After clicking Next, create the filename "Login". Anyway you can change the filename of your project. Note: filename and class name must be the same text and letter case. Then click Finish. jcreator 5. Now, delete the contents of your Login.java. Then copy/paste this code below.
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.sql.*;
  5. public class Login extends JFrame implements ActionListener{
  6.  
  7. public static void main(String[]args){
  8. setLogin();
  9. }
  10.  
  11. JFrame frame= new JFrame("Administrator Login");
  12. JLabel lbluser = new JLabel("Username:");
  13. JLabel lblpass = new JLabel("Password:");
  14. JTextField txtuser = new JTextField();
  15. JPasswordField txtpass = new JPasswordField();
  16. JButton btnok = new JButton(new ImageIcon("keys.gif"));
  17. JButton btnclose = new JButton(new ImageIcon("exits.png"));
  18. public String loginname="";
  19. public String loginpass="";
  20. public String loginname1="";
  21. public String user="";
  22. public String pass="";
  23. public int cnt;
  24. int dialogtype = JOptionPane.PLAIN_MESSAGE;
  25. String dialogmessage;
  26. String dialogs;
  27.  
  28.  
  29. public static void setLogin(){
  30. Login p1= new Login();
  31. p1.setSize(250,140);
  32. p1.setLocation(300,300);
  33. p1.setVisible(true);
  34. p1.setResizable(false);
  35. }
  36.  
  37.  
  38. public Login() {
  39. super("Administrator Login");
  40. JPanel pane= new JPanel();
  41. pane.setLayout(null);
  42.  
  43. pane.setBackground(Color.black);
  44. lbluser.setForeground(Color.white);
  45. lblpass.setForeground(Color.white);
  46.  
  47. pane.add(lbluser);
  48. lbluser.setBounds(5,5,100,20);
  49. pane.add(txtuser);
  50. txtuser.setBounds(110,5,100,20);
  51.  
  52. pane.add(lblpass);
  53. lblpass.setBounds(5,30,100,20);
  54. pane.add(txtpass);
  55. txtpass.setBounds(110,30,100,20);
  56.  
  57. pane.add(btnok);
  58. btnok.setBounds(120,65,40,35);
  59. btnok.addActionListener(this);
  60.  
  61. pane.add(btnclose);
  62. btnclose.setBounds(155,65,40,35);
  63. btnclose.addActionListener(this);
  64.  
  65. btnok.setToolTipText("Log-in");
  66. btnclose.setToolTipText("Exit");
  67.  
  68. setContentPane(pane);
  69. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  70. pane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), ""));
  71.  
  72. try{
  73. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  74. cn = DriverManager.getConnection("jdbc:odbc:LoginTutorial");
  75. JOptionPane.showMessageDialog(null,"unable to load driver");
  76. System.exit(0);
  77. }catch(SQLException e){
  78. JOptionPane.showMessageDialog(null,"unable to connect");
  79. System.exit(0);
  80. }
  81. }
  82. public void actionPerformed(ActionEvent e){
  83.  
  84. Object source=e.getSource();
  85. if(source==btnok)
  86. {
  87.  
  88. try{
  89. String str1=txtuser.getText();
  90. String str2=txtpass.getText();
  91. if((str1.length()==0 || str2.length()==0)){
  92. JOptionPane.showMessageDialog(null,"Some Fields are empty","WARNING",JOptionPane.WARNING_MESSAGE);
  93. }
  94. else{
  95. st=cn.createStatement();
  96. rs=st.executeQuery("SELECT * from Login Where username like'" +txtuser.getText() + "'AND password='"+txtpass.getText()+"'");
  97.  
  98. while(rs.next()){
  99. loginname=rs.getString("Username");
  100. loginpass=rs.getString("Password");
  101. loginname1=rs.getString("name1");
  102. }
  103. if((loginname.equalsIgnoreCase(txtuser.getText()))&&(loginpass.equalsIgnoreCase(txtpass.getText())))
  104. {
  105.  
  106. dialogmessage = "Welcome - " +loginname1;
  107. dialogtype = JOptionPane.INFORMATION_MESSAGE;
  108. JOptionPane.showMessageDialog((Component)null, dialogmessage, dialogs, dialogtype);
  109. txtuser.setText("");
  110. txtpass.setText("");
  111. dispose();
  112.  
  113. }
  114. else
  115. {
  116. JOptionPane.showMessageDialog(null, "INVALID ID OR PASSWORD!","WARNING!!",JOptionPane.WARNING_MESSAGE);
  117. cnt=cnt+1;
  118. txtuser.setText("");
  119. txtpass.setText("");
  120. }
  121.  
  122. if(cnt==3){
  123. frame.dispose();
  124. }
  125. }
  126. }catch(SQLException c){
  127. System.out.print(c.getMessage());
  128. }
  129.  
  130. }
  131.  
  132.  
  133. else
  134. {
  135. System.exit(0);
  136. }
  137. if(source==btnclose){
  138. dispose();
  139. }
  140. }
  141. }
Note: After putting the code, download the images below for the Login Button. iconicon 6. Now, we will create next the Database and we will use MS Access. Go to any version of Microsoft Access higher or equal to 2003 version. Create a the file name of sample.mdb. mdb extension files are 2003 version of MS Access. Create this tables and entities. accessdesign 7. Now, you will see the last text in our Driver Manager connection with this code below. Copy the text that I highlighted. design 8. Now, go to Start, then Click Control Panel. JCreator is also compatible to Windows 7 so no need to worry. I'm using Windows XP here. If you are using Windows 7 just find the Control Panel. I know you knew where it is :) design 9. Click the Administrative Tools. design 10. Click the Data Sources (ODBC). design 11. In the ODBC Data Source Administrator, click the Add button. design 12. Find Microsoft Access Driver (.mdb,.accdb) and then click Finish. design 13. In the Data Source Name, put LoginTutorial. This "LoginTutorial" was I told to be highlighted as this is the connection string of our program written in the code. Put also any description, and click Select button in the database tab. design 14. Locate the directory and Find the folder of your program if the database is in a folder. Just only click the folder or directory and it will come out in the database area. Then click Ok button. 15. Now, build the program by clicking the Build button. If there is no error, then click the Run Button! design You will see the following output: outputoutputoutput For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment