Question

This project will create a GUI to access data held in a MySQL database. You will design a GUI usi...

This project will create a GUI to access data held in a MySQL database. You will design a GUI using Swing components that will allow you to add a record, delete a record, update a record and display all current records int he database. The design is your choice. Extra credit will be given for the ability to search for a specific record by a key value. You will need to provide details of the database domain, table and record structure
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class MySQLSwing implements ActionListener {

   JTextField txtFName,txtLName,txtAddress,txtSalary;
   JButton btnAdd,btnUpdate,btnDelete,btnPrev,btnNext;
   JLabel lblFName,lblLname,lblAddress,lblSalary,lblF,lblL,lblA,lblS,lblFVal,lblLVal,lblAVal,lblSVal;
   ResultSet rs;

   private void displayUI()
   {
       JFrame frame = new JFrame("GUI to access data");
JPanel panel = new JPanel(new GridLayout(4,2));

       lblFName = new JLabel(" First Name : ");
       txtFName = new JTextField(15);

       lblLname = new JLabel(" Last Name : ");
       txtLName = new JTextField();

       lblAddress = new JLabel(" Address : ");
       txtAddress = new JTextField();

       lblSalary = new JLabel(" Salary : ");
       txtSalary = new JTextField();

       panel.add(lblFName);
       panel.add(txtFName);

       panel.add(lblLname);
       panel.add(txtLName);

       panel.add(lblAddress);
       panel.add(txtAddress);

       panel.add(lblSalary);
       panel.add(txtSalary);

       JPanel pnlButton = new JPanel(new GridLayout(1,3));

       btnAdd = new JButton("Add");
       btnAdd.addActionListener(this);

       btnUpdate = new JButton("Update");
       btnUpdate.addActionListener(this);

       btnDelete = new JButton("Delete");
       btnDelete.addActionListener(this);

       pnlButton.add(btnAdd);
       pnlButton.add(btnUpdate);
       pnlButton.add(btnDelete);

       JPanel pnlNavigate = new JPanel(new GridLayout(1,2));
       btnPrev = new JButton(" << ");
       btnPrev.setActionCommand("Prev");
       btnPrev.addActionListener(this);

       btnNext = new JButton(" >> ");
       btnNext.setActionCommand("Next");
       btnNext.addActionListener(this);

       pnlNavigate.add(btnPrev);
       pnlNavigate.add(btnNext);

       JPanel pnlNavAns = new JPanel(new GridLayout(4,2));

       lblF = new JLabel(" First Name : ");
       lblFVal = new JLabel("Val");

       lblL = new JLabel(" Last Name : ");
       lblLVal = new JLabel("Val");

       lblA = new JLabel(" Address : ");
       lblAVal = new JLabel("Val");

       lblS = new JLabel(" Salary : ");
       lblSVal = new JLabel("Val");

       pnlNavAns.add(lblF);
       pnlNavAns.add(lblFVal);

       pnlNavAns.add(lblL);
       pnlNavAns.add(lblLVal);

       pnlNavAns.add(lblA);
       pnlNavAns.add(lblAVal);

       pnlNavAns.add(lblS);
       pnlNavAns.add(lblSVal);

       Container cn = frame.getContentPane();
       cn.setLayout(new BoxLayout(cn,BoxLayout.Y_AXIS));

       frame.add(panel);
       frame.add(pnlButton);
       frame.add(pnlNavAns);
       frame.add(pnlNavigate);

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.pack();
       frame.setVisible(true);
   }
  
   @Override
   public void actionPerformed(ActionEvent evt) {

       String action = evt.getActionCommand();
       if(action.equals("Add"))
       {
           insertOperation();
       }else if(action.equals("Update"))
       {
           updateOperation();
       }else if(action.equals("Delete"))
       {
           deleteOperation();
       }else if(action.equals("Prev"))
       {
           preNavigation();
       }else if(action.equals("Next"))
       {
           nextNavigation();
       }
   }
  
  
   private void insertOperation()
   {
       try
       {
           //Load Jdbc Odbc Driver
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           Connection con = DriverManager.getConnection("jdbc:odbc:ShivaEvening");

           String sql = "INSERT INTO Employee (FName,LName,Address,Salary) " +
                   "Values ('"+txtFName.getText()+"'," +
                           "'"+txtLName.getText()+"'," +
                           "'"+txtAddress.getText()+"'," +
                           "'"+txtSalary.getText()+"')";

           Statement st = con.createStatement();
           st.execute(sql);

           JOptionPane.showMessageDialog(null, "Record Added Succesfully.","Record Added",
JOptionPane.INFORMATION_MESSAGE);
           clearControls();
       }catch(Exception e)
       {
           JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
JOptionPane.ERROR_MESSAGE);
       }
   }
  
  
   private void updateOperation()
   {
       try
       {
           //Load Jdbc Odbc Driver
           Class.forName("com.mysql.jdbc.Driver");
           Connection con=DriverManager.getConnection(
                   "jdbc:mysql://localhost:3306/sonoo","root","root");

           String sql = "Update Employee " +
                   "SET LName = '"+txtLName.getText()+"'," +
                           "Address = '"+txtAddress.getText()+"'," +
                           "Salary = '"+txtSalary.getText()+"'" +
                           "Where FName = '"+txtFName.getText()+"'";

           JOptionPane.showMessageDialog(null, sql,"Record Updated",
JOptionPane.INFORMATION_MESSAGE);
           Statement st = con.createStatement();
           st.execute(sql);

           JOptionPane.showMessageDialog(null, "Record Update Succesfully.",
"Record Updated",JOptionPane.INFORMATION_MESSAGE);
           clearControls();
       }catch(Exception e)
       {
           JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
JOptionPane.ERROR_MESSAGE);
       }
   }
  
  
   private void deleteOperation()
   {
       int ans = JOptionPane.showConfirmDialog(null,
               "Are you sure to delete the Record ?", "Delete Record",
JOptionPane.YES_NO_OPTION);
       if(ans == JOptionPane.YES_OPTION)
       {
           try{
           //Load Jdbc Odbc Driver
               Class.forName("com.mysql.jdbc.Driver");
               Connection con=DriverManager.getConnection(
                       "jdbc:mysql://localhost:3306/sonoo","root","root");

           String sql = "Delete FROM Employee where FName = '"+txtFName.getText()+"'";

           Statement st = con.createStatement();
           st.execute(sql);
           }catch(Exception e)
           {
               JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
JOptionPane.ERROR_MESSAGE);
           }
           JOptionPane.showMessageDialog(null, "Record Deleted","Success",
JOptionPane.INFORMATION_MESSAGE);
       }
       else
       {
           JOptionPane.showMessageDialog(null, "Operation Canceled","Cancel",
JOptionPane.INFORMATION_MESSAGE);
       }
   }
  
  
   private void preNavigation()
   {
       try{
           if(rs == null)
           {
           //Load Jdbc Odbc Driver
               Class.forName("com.mysql.jdbc.Driver");
               Connection con=DriverManager.getConnection(
                       "jdbc:mysql://localhost:3306/sonoo","root","root");

           String sql = "SELECT * FROM Employee";

           Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
           rs = st.executeQuery(sql);
           }
           if(rs.previous())
           {
               populateValue();
           }
           }catch(Exception e)
           {
               JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
JOptionPane.ERROR_MESSAGE);
           }
   }
  
  
   private void nextNavigation()
   {
       try{
           if(rs == null)
           {
           //Load Jdbc Odbc Driver
           Class.forName("com.mysql.jdbc.Driver");
           Connection con=DriverManager.getConnection(
                   "jdbc:mysql://localhost:3306/sonoo","root","root");
          
           String sql = "SELECT * FROM Employee";

           Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
           rs = st.executeQuery(sql);
           }
           if(rs.next())
           {
               populateValue();
           }
           }catch(Exception e)
           {
               JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
JOptionPane.ERROR_MESSAGE);
           }
   }
  
   private void populateValue() throws Exception
   {
       String fName = rs.getString("FName");
       String lName = rs.getString("LName");
       String add = rs.getString("Address");
       String sal = rs.getString("Salary");

       lblFVal.setText(fName);
       lblLVal.setText(lName);
       lblAVal.setText(add);
       lblSVal.setText(sal);

       txtFName.setText(fName);
       txtLName.setText(lName);
       txtAddress.setText(add);
       txtSalary.setText(sal);
   }
  
   private void clearControls()
   {
       txtFName.setText("");
       txtLName.setText("");
       txtAddress.setText("");
       txtSalary.setText("");
   }
  
  
   public static void main(String[] args) {
       MySQLSwing obj = new MySQLSwing();
       obj.displayUI();
   }
}

D MySQLSwing.java 3 Button btnAdd, btnUpdate,btnDelete,btnPrev, btnNext; 12 13 14 15 private void displayUI() ResultSet rs; J

Add a comment
Know the answer?
Add Answer to:
This project will create a GUI to access data held in a MySQL database. You will design a GUI usi...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using the MySQL Workbench create a new database using your design specifications Add at least 10...

    Using the MySQL Workbench create a new database using your design specifications Add at least 10 records to your tables. Note: Certain tables may not require 10 records and that is ok as long as your main tables have 10 or more Create MySQL statements that will retrieve all records or rows from the tables in your database Create 10 MySQL statements that will retrieve specified records or rows from one table in your database Create 10 MySQL statements that...

  • Using MySQL and PHP keep it simple This assignment you will be making a form that...

    Using MySQL and PHP keep it simple This assignment you will be making a form that will do one of three things in a database -          It will add a record -          It will update a record -          It will search for a record Your database will contain a table for keeping a record of all your friends and family and should contain: First name Last name Phone number Address City State Zip Birthdate Username Password The sex of the...

  • Programming Microsoft Visual Basic 2012/2015 - Create an SQL or Access database with all your friends’...

    Programming Microsoft Visual Basic 2012/2015 - Create an SQL or Access database with all your friends’ information and write a program to load the names in the combo box and also search the database and find your friend’s record off the database by name and retrieve all the information from the database to the form including your friend’s picture. You should be able to add new friends, update an existing friends’ information and delete a friend’s record. See the sample...

  • I want solution by wampserver 4. Database Programming [8 Marks) a. Create a database in wampserver...

    I want solution by wampserver 4. Database Programming [8 Marks) a. Create a database in wampserver with your National ID. b. Create the table structure with your college ID Number, for the all fields with the proper filed name, data type, size and constraint. c. Modify your php program with necessary mysql database connection. d. When you click register button [in the registration form], save the record into the database and display the result in table format. c. When you...

  • Overview: Database management plays an integral role in nearly every area of business. Databases house customer, accoun...

    Overview: Database management plays an integral role in nearly every area of business. Databases house customer, accounting, and employee data, and these different data sets must all be efficiently managed in order to make the data accessible. Companies rely on database engineers to ensure that their records are accurate, updated, and tracked in real time. This course covers structured query language (SQL) and how it can be used to manage database schemas, manipulate data, and analyze data. For your final...

  • Using C# Create a database that stores baseball player information. You will have the following columns....

    Using C# Create a database that stores baseball player information. You will have the following columns. You will need to have the ability to add rows and display the records. You may use LINQ or Given example using an access accdb file... Name string Batting Average floating, decimal, double team string jersey number int ID autonumber

  • You will create an Microsoft Access School Management System Database that can be used to store,...

    You will create an Microsoft Access School Management System Database that can be used to store, retrieve update and delete the staff/student. Design an Access database to maintain information about school staff and students satisfying the following properties: 1. The staff should have the following: ID#, name, and classes they are teaching 2. The student should have the following: ID#, name, section, class 3. Create a module containing the section, subject and teacher information 4. Create a module containing student...

  • Project Description In this project, you will design and implement a database for keeping track of...

    Project Description In this project, you will design and implement a database for keeping track of information for an online “SOCIAL NETWORK” system (e.g. a simplified version of Facebook!). You will first design an EER schema diagram for this database application. Then, you will map the EER schema into a relational database schema and implement it on ORACLE or MySQL or some other relational DBMS. Finally, you will load some data into your database (via user Interface) and create some...

  • Create a Web page to be used for storing software development bug reports in a MySQL...

    Create a Web page to be used for storing software development bug reports in a MySQL database. Include fields such as product name and version, type of hardware, operating system, frequency of occurrence, and proposed solutions. On the main page you display all the reported bugs (hint: use a table). Include a link at the bottom of the page which takes you to another page where you can create a new bug report. Furthermore, add a link after every filed...

  • please use dia to draw the e-r diagram to create Entity - Relationship Diagrams then use MS access This project requires you to create a database design. Your design will be documented in a set o...

    please use dia to draw the e-r diagram to create Entity - Relationship Diagrams then use MS access This project requires you to create a database design. Your design will be documented in a set of Entity-Relationship Crow's Foot diagrams using the representation as shown in the lecture materials. Draw a set of Entity-Relationship diagrams to model the following scenario. The Maggs Realty Company wants to track the Sales Offices that they have across all of Ontario They are interested...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT