Creating User Account Information in Java - Searching Records to Database

Submitted by: 
Language: 
Visitors have accessed this post 717 times.

This is a continuation of my last tutorials entitled Creating User Account Information in Java - Adding Records to Database. So, in this tutorial we will create a function button that provides searching of records in the access database.

Now let's start this tutorial! :)

1. Create your database in ms access and named it sample.mdb with Login table and the following entities below.

designdesign

2. Create your java program now named UserSettings.java.

3. Import the following packages.

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.sql.*;
  5. import java.lang.*;

4. Initialize the following variables add only the btnSearch Button in our last tutorial.

  1. JLabel lblUser = new JLabel("Username ",JLabel.RIGHT);
  2. JLabel lblPass = new JLabel("Password ",JLabel.RIGHT);
  3. JLabel lblName1 = new JLabel("First Name",JLabel.RIGHT);
  4. JLabel lblName2 = new JLabel("Family Name",JLabel.RIGHT);
  5.  
  6. JTextField txtUser = new JTextField(20);
  7. JPasswordField txtPass= new JPasswordField(20);
  8. JTextField txtName1= new JTextField(20);
  9. JTextField txtName2= new JTextField(20);
  10.  
  11. JButton btnNew = new JButton("Add");
  12. JButton btnSearch = new JButton("Search");
  13.  

5. Create a constructor named UserSettings() same with your classname for creating the panels to put controls in the form as well as the connection in the database. This will add the btnSearch Button in the panel.

  1. public UserSettings() {
  2. super("User Account Settings");
  3.  
  4. JPanel pane = new JPanel();
  5. pane.setLayout(null);
  6.  
  7. lblUser.setBounds(5,50,80,25);
  8. pane.add(lblUser);
  9. txtUser.setBounds(90,50,150,25);
  10. pane.add(txtUser);
  11. lblUser.setForeground(Color.white);
  12.  
  13. lblPass.setBounds(5,85,80,25);
  14. pane.add(lblPass);
  15. txtPass.setBounds(90,85,150,25);
  16. txtPass.setEchoChar('*');
  17. pane.add(txtPass);
  18. lblPass.setForeground(Color.white);
  19.  
  20. lblName1.setBounds(5,120,80,25);
  21. pane.add(lblName1);
  22. txtName1.setBounds(90,120,150,25);
  23. pane.add(txtName1);
  24. lblName1.setForeground(Color.white);
  25.  
  26.  
  27. lblName2.setBounds(5,155,80,25);
  28. pane.add(lblName2);
  29. txtName2.setBounds(90,155,150,25);
  30. pane.add(txtName2);
  31. lblName2.setForeground(Color.white);
  32.  
  33. btnNew.setBounds(5,190,75,35);
  34. pane.add(btnNew);
  35.  
  36. btnNew.addActionListener(this);
  37.  
  38. btnSearch.setBounds(230,190,75,35);
  39. pane.add(btnSearch);
  40.  
  41. btnSearch.addActionListener(this);
  42.  
  43.  
  44. pane.setBackground(Color.black);
  45.  
  46.  
  47. setContentPane(pane);
  48. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  49. pane.setBorder(BorderFactory.createTitledBorder(
  50. BorderFactory.createEtchedBorder(), "Creating User Account"));
  51.  
  52. try{
  53. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  54. cn = DriverManager.getConnection("jdbc:odbc:User");
  55. System.err.println("Failed to load driver");
  56. e.printStackTrace();
  57. }
  58. catch(SQLException e){
  59. System.err.println("Unable to connect");
  60. e.printStackTrace();
  61. }
  62. }

7. Create your ActionEvent for clicking the button btnSearch. This will trigger to search the records in the database. If found, it will display the records in the textfields. Otherwise, it will prompt the user that the record is not found.

  1. public void actionPerformed(ActionEvent e){
  2. Object source = e.getSource();
  3. if(source == btnSearch){
  4. try{
  5.  
  6. String sUser ="";
  7. int tmp= 0;
  8. clear();
  9. sUser = JOptionPane.showInputDialog(null,"Enter Username to search.","Payroll System: User settings",JOptionPane.QUESTION_MESSAGE);
  10. st= cn.createStatement();
  11. ResultSet rs=st.executeQuery("SELECT * FROM Login WHERE username = '" + sUser + "'");
  12.  
  13. while(rs.next()){
  14. txtUser.setText(rs.getString(1));
  15. txtPass.setText(rs.getString(2));
  16. txtName1.setText(rs.getString(3));
  17. txtName2.setText(rs.getString(4));
  18. tmp=1;
  19. }
  20. st.close();
  21. if (tmp==0){
  22. JOptionPane.showMessageDialog(null,"No record found!!.","Payroll System: User settings",JOptionPane.INFORMATION_MESSAGE);
  23. }
  24. }catch(SQLException s){
  25. JOptionPane.showMessageDialog(null,"Unable to search!.","Payroll System: User settings",JOptionPane.ERROR_MESSAGE);
  26. System.out.println("SQL Error" + s.toString() + " " + s.getErrorCode() + " " + s.getSQLState());
  27. }
  28. }
  29. }

The sql syntax here "ResultSet rs=st.executeQuery("SELECT * FROM Login WHERE username = '" + sUser + "'");" searches the record in the database and will display the records in the textfield with the following code:

  1. while(rs.next()){
  2. txtUser.setText(rs.getString(1));
  3. txtPass.setText(rs.getString(2));
  4. txtName1.setText(rs.getString(3));
  5. txtName2.setText(rs.getString(4));
  6. tmp=1;
  7. }

8. Create a method named clear to clear all your textfield.

  1. public void clear(){
  2. txtUser.setText("");
  3. txtPass.setText("");
  4. txtName1.setText("");
  5. txtName2.setText("");
  6. }

9. Lastly create your Main. This will create the size and location of your form.

  1. public static void main(String[]args){
  2. UserSettings panel = new UserSettings();
  3. panel.setSize(370,350);
  4. panel.setVisible(true);
  5. panel.setLocation(350,200);
  6. panel.setResizable(false);
  7. }

Output:

outputoutputoutput
Here's the full code of this tutorial:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.sql.*;
  5. import java.lang.*;
  6.  
  7. public class UserSettings extends JFrame implements ActionListener{
  8.  
  9. JLabel lblUser = new JLabel("Username ",JLabel.RIGHT);
  10. JLabel lblPass = new JLabel("Password ",JLabel.RIGHT);
  11. JLabel lblName1 = new JLabel("First Name",JLabel.RIGHT);
  12. JLabel lblName2 = new JLabel("Family Name",JLabel.RIGHT);
  13.  
  14. JTextField txtUser = new JTextField(20);
  15. JPasswordField txtPass= new JPasswordField(20);
  16. JTextField txtName1= new JTextField(20);
  17. JTextField txtName2= new JTextField(20);
  18.  
  19. JButton btnNew = new JButton("Add");
  20. JButton btnSearch = new JButton("Search");
  21.  
  22.  
  23.  
  24. public void clear(){
  25. txtUser.setText("");
  26. txtPass.setText("");
  27. txtName1.setText("");
  28. txtName2.setText("");
  29. }
  30. public UserSettings() {
  31. super("User Account Settings");
  32.  
  33. JPanel pane = new JPanel();
  34. pane.setLayout(null);
  35.  
  36. lblUser.setBounds(5,50,80,25);
  37. pane.add(lblUser);
  38. txtUser.setBounds(90,50,150,25);
  39. pane.add(txtUser);
  40. lblUser.setForeground(Color.white);
  41.  
  42. lblPass.setBounds(5,85,80,25);
  43. pane.add(lblPass);
  44. txtPass.setBounds(90,85,150,25);
  45. txtPass.setEchoChar('*');
  46. pane.add(txtPass);
  47. lblPass.setForeground(Color.white);
  48.  
  49. lblName1.setBounds(5,120,80,25);
  50. pane.add(lblName1);
  51. txtName1.setBounds(90,120,150,25);
  52. pane.add(txtName1);
  53. lblName1.setForeground(Color.white);
  54.  
  55.  
  56. lblName2.setBounds(5,155,80,25);
  57. pane.add(lblName2);
  58. txtName2.setBounds(90,155,150,25);
  59. pane.add(txtName2);
  60. lblName2.setForeground(Color.white);
  61.  
  62. btnNew.setBounds(5,190,75,35);
  63. pane.add(btnNew);
  64.  
  65. btnNew.addActionListener(this);
  66.  
  67. btnSearch.setBounds(230,190,75,35);
  68. pane.add(btnSearch);
  69.  
  70. btnSearch.addActionListener(this);
  71.  
  72.  
  73. pane.setBackground(Color.black);
  74.  
  75.  
  76. setContentPane(pane);
  77. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  78. pane.setBorder(BorderFactory.createTitledBorder(
  79. BorderFactory.createEtchedBorder(), "Creating User Account"));
  80.  
  81. try{
  82. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  83. cn = DriverManager.getConnection("jdbc:odbc:User");
  84. System.err.println("Failed to load driver");
  85. e.printStackTrace();
  86. }
  87. catch(SQLException e){
  88. System.err.println("Unable to connect");
  89. e.printStackTrace();
  90. }
  91. }
  92.  
  93. public void actionPerformed(ActionEvent e){
  94. Object source = e.getSource();
  95.  
  96. if(source == btnSearch){
  97. try{
  98.  
  99. String sUser ="";
  100. int tmp= 0;
  101. clear();
  102. sUser = JOptionPane.showInputDialog(null,"Enter Username to search.","Payroll System: User settings",JOptionPane.QUESTION_MESSAGE);
  103. st= cn.createStatement();
  104. ResultSet rs=st.executeQuery("SELECT * FROM Login WHERE username = '" + sUser + "'");
  105.  
  106. while(rs.next()){
  107. txtUser.setText(rs.getString(1));
  108. txtPass.setText(rs.getString(2));
  109. txtName1.setText(rs.getString(3));
  110. txtName2.setText(rs.getString(4));
  111. tmp=1;
  112. }
  113. st.close();
  114. if (tmp==0){
  115. JOptionPane.showMessageDialog(null,"No record found!!.","Payroll System: User settings",JOptionPane.INFORMATION_MESSAGE);
  116. }
  117. }catch(SQLException s){
  118. JOptionPane.showMessageDialog(null,"Unable to search!.","Payroll System: User settings",JOptionPane.ERROR_MESSAGE);
  119. System.out.println("SQL Error" + s.toString() + " " + s.getErrorCode() + " " + s.getSQLState());
  120. }
  121. }
  122. }
  123. // public void frameUser(){
  124. public static void main(String[]args){
  125. UserSettings panel = new UserSettings();
  126. panel.setSize(370,350);
  127. panel.setVisible(true);
  128. panel.setLocation(350,200);
  129. panel.setResizable(false);
  130. }
  131. }

Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add and Follow me on Facebook: https://www.facebook.com/donzzsky


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.