Creating User Account Information in Java - Searching Records to Database
Submitted by donbermoy on Thursday, May 1, 2014 - 13:27.
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.
2. Create your java program now named UserSettings.java.
3. Import the following packages.
4. Initialize the following variables add only the btnSearch Button in our last tutorial.
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.
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.
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:
8. Create a method named clear to clear all your textfield.
9. Lastly create your Main. This will create the size and location of your form.
Output:
Here's the full code of this tutorial:
Best Regards,
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.*;
- import java.lang.*;
- Connection cn;
- Statement st;
- public UserSettings() {
- super("User Account Settings");
- pane.setLayout(null);
- lblUser.setBounds(5,50,80,25);
- pane.add(lblUser);
- txtUser.setBounds(90,50,150,25);
- pane.add(txtUser);
- lblPass.setBounds(5,85,80,25);
- pane.add(lblPass);
- txtPass.setBounds(90,85,150,25);
- txtPass.setEchoChar('*');
- pane.add(txtPass);
- lblName1.setBounds(5,120,80,25);
- pane.add(lblName1);
- txtName1.setBounds(90,120,150,25);
- pane.add(txtName1);
- lblName2.setBounds(5,155,80,25);
- pane.add(lblName2);
- txtName2.setBounds(90,155,150,25);
- pane.add(txtName2);
- btnNew.setBounds(5,190,75,35);
- pane.add(btnNew);
- btnNew.addActionListener(this);
- btnSearch.setBounds(230,190,75,35);
- pane.add(btnSearch);
- btnSearch.addActionListener(this);
- setContentPane(pane);
- try{
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- e.printStackTrace();
- }
- e.printStackTrace();
- }
- }
- if(source == btnSearch){
- try{
- int tmp= 0;
- clear();
- sUser = JOptionPane.showInputDialog(null,"Enter Username to search.","Payroll System: User settings",JOptionPane.QUESTION_MESSAGE);
- st= cn.createStatement();
- while(rs.next()){
- txtUser.setText(rs.getString(1));
- txtPass.setText(rs.getString(2));
- txtName1.setText(rs.getString(3));
- txtName2.setText(rs.getString(4));
- tmp=1;
- }
- st.close();
- if (tmp==0){
- JOptionPane.showMessageDialog(null,"No record found!!.","Payroll System: User settings",JOptionPane.INFORMATION_MESSAGE);
- }
- JOptionPane.showMessageDialog(null,"Unable to search!.","Payroll System: User settings",JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- while(rs.next()){
- txtUser.setText(rs.getString(1));
- txtPass.setText(rs.getString(2));
- txtName1.setText(rs.getString(3));
- txtName2.setText(rs.getString(4));
- tmp=1;
- }
- public void clear(){
- txtUser.setText("");
- txtPass.setText("");
- txtName1.setText("");
- txtName2.setText("");
- }
- UserSettings panel = new UserSettings();
- panel.setSize(370,350);
- panel.setVisible(true);
- panel.setLocation(350,200);
- panel.setResizable(false);
- }
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.*;
- import java.lang.*;
- Connection cn;
- Statement st;
- public void clear(){
- txtUser.setText("");
- txtPass.setText("");
- txtName1.setText("");
- txtName2.setText("");
- }
- public UserSettings() {
- super("User Account Settings");
- pane.setLayout(null);
- lblUser.setBounds(5,50,80,25);
- pane.add(lblUser);
- txtUser.setBounds(90,50,150,25);
- pane.add(txtUser);
- lblPass.setBounds(5,85,80,25);
- pane.add(lblPass);
- txtPass.setBounds(90,85,150,25);
- txtPass.setEchoChar('*');
- pane.add(txtPass);
- lblName1.setBounds(5,120,80,25);
- pane.add(lblName1);
- txtName1.setBounds(90,120,150,25);
- pane.add(txtName1);
- lblName2.setBounds(5,155,80,25);
- pane.add(lblName2);
- txtName2.setBounds(90,155,150,25);
- pane.add(txtName2);
- btnNew.setBounds(5,190,75,35);
- pane.add(btnNew);
- btnNew.addActionListener(this);
- btnSearch.setBounds(230,190,75,35);
- pane.add(btnSearch);
- btnSearch.addActionListener(this);
- setContentPane(pane);
- try{
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- e.printStackTrace();
- }
- e.printStackTrace();
- }
- }
- if(source == btnSearch){
- try{
- int tmp= 0;
- clear();
- sUser = JOptionPane.showInputDialog(null,"Enter Username to search.","Payroll System: User settings",JOptionPane.QUESTION_MESSAGE);
- st= cn.createStatement();
- while(rs.next()){
- txtUser.setText(rs.getString(1));
- txtPass.setText(rs.getString(2));
- txtName1.setText(rs.getString(3));
- txtName2.setText(rs.getString(4));
- tmp=1;
- }
- st.close();
- if (tmp==0){
- JOptionPane.showMessageDialog(null,"No record found!!.","Payroll System: User settings",JOptionPane.INFORMATION_MESSAGE);
- }
- JOptionPane.showMessageDialog(null,"Unable to search!.","Payroll System: User settings",JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- // public void frameUser(){
- UserSettings panel = new UserSettings();
- panel.setSize(370,350);
- panel.setVisible(true);
- panel.setLocation(350,200);
- panel.setResizable(false);
- }
- }
Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]
Visit and like my page on Facebook at: Bermz ISware Solutions
Subscribe at my YouTube Channel at: SerBermz
Add new comment
- 90 views