Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java help! I need my search button (SrcBtn) and its text field (JTSearch) wired

ID: 3670726 • Letter: J

Question

Java help!

I need my search button (SrcBtn) and its text field (JTSearch) wired up, it needs to be able to seach for items in my database by LName. Also, I need the menu items to work too please. Exit will exit the program, and about will create a pop up with (for now) a "Hello" message.

import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class GUI extends JFrame
{
   static Connection Con;
   static Statement St;
   static ResultSet RS;
   private JLabel JLFName;
   private JLabel JLLName;
   private JLabel JLAge;
   private JTextField JTFName;
   private JTextField JTLName;
   private JTextField JTAge;
   private JTextField JTSearch;
   private JButton NxtBtn, PrvBtn, FstBtn, LstBtn,
   UpdBtn,DelBtn,AddBtn,SveBtn, SrcBtn;
   private static final int WIDTH = 400;
   private static final int HEIGHT = 300;
private JMenuBar BarMenu;
private JMenu FileM, HelpM;
private JMenuItem Exit, About;

  
   public GUI()
   {
       NxtBtn = new JButton("Next");
       PrvBtn = new JButton("Previous");
       FstBtn = new JButton("First");
       LstBtn = new JButton("Last");
       UpdBtn = new JButton("Update");
       DelBtn = new JButton("Delete");
       AddBtn = new JButton("Add");
       SveBtn = new JButton("Save");
       SrcBtn = new JButton("Search");
       BarMenu = new JMenuBar();
       setJMenuBar(BarMenu);
       FileM = new JMenu("File");
       HelpM = new JMenu("Help");
       BarMenu.add(FileM);
       Exit = new JMenuItem("Exit");
       FileM.add(Exit);
       BarMenu.add(HelpM);
       About = new JMenuItem("About");
       HelpM.add(About);
      
       Container MyContain = getContentPane();
       MyContain.setLayout(new GridLayout(8, 8));
       JLFName = new JLabel("First Name");
       JLLName = new JLabel("Last Name");
       JLAge = new JLabel("Age Name");
       JTFName = new JTextField(10);
       JTLName = new JTextField(10);
       JTAge = new JTextField(10);
       JTSearch = new JTextField(10);
       //JPanel JP = new JPanel();
       MyContain.add(JLFName);
       MyContain.add(JTFName);
       MyContain.add(JLLName);
       MyContain.add(JTLName);
       MyContain.add(JLAge);
       MyContain.add(JTAge);
       MyContain.add(NxtBtn);
       MyContain.add(PrvBtn);
       MyContain.add(FstBtn);
       MyContain.add(LstBtn);
       MyContain.add(UpdBtn);
       MyContain.add(DelBtn);
       MyContain.add(AddBtn);
       MyContain.add(SveBtn);
       MyContain.add(SrcBtn);
       MyContain.add(JTSearch);
      
       setSize(WIDTH, HEIGHT);
       setVisible(true);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       Frame();
       BtnAction();
   }

   public static void Connect()
   {
       try
       {
           String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
           Class.forName(driver);
           String Database = "jdbc:odbc:MyDB";
           Con = DriverManager.getConnection(Database);
           St = Con.createStatement(
                   ResultSet.TYPE_SCROLL_INSENSITIVE,
                   ResultSet.CONCUR_UPDATABLE);
           String sql = "select * from MyTable";
           RS = St.executeQuery(sql);
       }
       catch(Exception ex)
       {
           System.out.println(ex);
       }
   }

  
  
   public static void main(String[] args)
   {
       Connect();
       new GUI();
   }

   public void Frame()
   {
       try
       {
           RS.next();
           JTFName.setText(RS.getString("FName"));
           JTLName.setText(RS.getString("LName"));
           JTAge.setText(RS.getString("Age"));
       }
       catch(Exception ex)
       {  
       }  
   }
   public void BtnAction()
   {
       System.out.println("Called");
       NxtBtn.addActionListener(new ActionListener()
       {
           public void actionPerformed(ActionEvent e)
           {
              
               try
               {
                   if(RS.next())
                   {
                       JTFName.setText(RS.getString("FName"));
                       JTLName.setText(RS.getString("LName"));
                       JTAge.setText(RS.getString("Age"));
                   }
                   else
                   {
                       RS.previous();
                       JOptionPane.showMessageDialog
                       (null, "No more records");
                   }
               }
               catch(Exception ex)
               {      
               }
           }
       });
       PrvBtn.addActionListener(new ActionListener()
       {
           public void actionPerformed(ActionEvent e)
           {
               try
               {
                   if(RS.previous())
                   {
                       JTFName.setText(RS.getString("FName"));
                       JTLName.setText(RS.getString("LName"));
                       JTAge.setText(RS.getString("Age"));
                   }
                   else
                   {
                       RS.next();
                       JOptionPane.showMessageDialog
                       (null, "No more records");
                   }
               }
               catch(Exception ex)
               {      
               }
           }
       });
       LstBtn.addActionListener(new ActionListener()
       {
           public void actionPerformed(ActionEvent e)
           {
               try
               {
                   RS.last();  
                   JTFName.setText(RS.getString("FName"));
                   JTLName.setText(RS.getString("LName"));
                   JTAge.setText(RS.getString("Age"));
               }
               catch(Exception ex)
               {      
               }
           }
       });
       FstBtn.addActionListener(new ActionListener()
       {
           public void actionPerformed(ActionEvent e)
           {
               try
               {
                   RS.first();  
                   JTFName.setText(RS.getString("FName"));
                   JTLName.setText(RS.getString("LName"));
                   JTAge.setText(RS.getString("Age"));
               }
               catch(Exception ex)
               {      
               }
           }
       });
       UpdBtn.addActionListener(new ActionListener()
       {
           public void actionPerformed(ActionEvent e)
           {
               String FName = JTFName.getText();
               String LName = JTLName.getText();
               String Age = JTAge.getText();
               try
               {
                   RS.updateString("FName", FName);
                   RS.updateString("LName", LName);
                   RS.updateString("Age", Age);
                   RS.updateRow();  
                   JOptionPane.showMessageDialog
                   (null, "Record Updated");
               }
               catch(Exception ex)
               {      
               }
           }
       });
       DelBtn.addActionListener(new ActionListener()
       {
           public void actionPerformed(ActionEvent e)
           {
               try
               {
                   RS.deleteRow();
                   St.close();
                   RS.close();
                   St = Con.createStatement(
                       ResultSet.TYPE_SCROLL_INSENSITIVE,
                       ResultSet.CONCUR_UPDATABLE);
                   String sql = "select * from MyTable";
                   RS = St.executeQuery(sql);
                   RS.next();
                   JTFName.setText(RS.getString("FName"));
                   JTLName.setText(RS.getString("LName"));
                   JTAge.setText(RS.getString("Age"));  
               }
               catch(Exception ex)
               {      
               }
           }
       });
       AddBtn.addActionListener(new ActionListener()
       {
           public void actionPerformed(ActionEvent e)
           {
               JTFName.setText(" ");
               JTLName.setText(" ");
               JTAge.setText(" ");
           }
       });
       SveBtn.addActionListener(new ActionListener()
       {
           public void actionPerformed(ActionEvent e)
           {
               String FName = JTFName.getText();
               String LName = JTLName.getText();
               String Age = JTAge.getText();
               try
               {
                   RS.moveToInsertRow();
                   RS.updateString("FName", FName);
                   RS.updateString("LName", LName);
                   RS.updateString("Age", Age);
                   RS.insertRow();
                   St.close();
                   RS.close();
                   St = Con.createStatement(
                       ResultSet.TYPE_SCROLL_INSENSITIVE,
                       ResultSet.CONCUR_UPDATABLE);
                   String sql = "select * from MyTable";
                   RS = St.executeQuery(sql);
                   RS.next();
                   JTFName.setText(RS.getString("FName"));
                   JTLName.setText(RS.getString("LName"));
                   JTAge.setText(RS.getString("Age"));
               }
               catch(Exception ex)
               {      
               }
           }
       });
       SrcBtn.addActionListener(new ActionListener()
       {
           public void actionPerformed(ActionEvent e)
           {
               try
               {
               }
               catch(Exception ex)
               {      
               }
           }
       });

   }  
}

Explanation / Answer

Checkout the below code:- (you have to make sure your database connections are good,you should also work on your logic )

import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class exepro extends JFrame
{
static Connection Con;
static Statement St,St1;
static ResultSet RS,RS1;
private JLabel JLFName;
private JLabel JLLName;
private JLabel JLAge;
private JTextField JTFName;
private JTextField JTLName;
private JTextField JTAge;
private JTextField JTSearch;
private JButton NxtBtn, PrvBtn, FstBtn, LstBtn,
UpdBtn,DelBtn,AddBtn,SveBtn, SrcBtn;
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
private JMenuBar BarMenu;
private JMenu FileM, HelpM;
private JMenuItem Exit, About;
  
public exepro()
{
NxtBtn = new JButton("Next");
PrvBtn = new JButton("Previous");
FstBtn = new JButton("First");
LstBtn = new JButton("Last");
UpdBtn = new JButton("Update");
DelBtn = new JButton("Delete");
AddBtn = new JButton("Add");
SveBtn = new JButton("Save");
SrcBtn = new JButton("Search");
BarMenu = new JMenuBar();
setJMenuBar(BarMenu);
FileM = new JMenu("File");
HelpM = new JMenu("Help");
BarMenu.add(FileM);
Exit = new JMenuItem("Exit");
FileM.add(Exit);
BarMenu.add(HelpM);
About = new JMenuItem("About");
HelpM.add(About);
  
Container MyContain = getContentPane();
MyContain.setLayout(new GridLayout(8, 8));
JLFName = new JLabel("First Name");
JLLName = new JLabel("Last Name");
JLAge = new JLabel("Age Name");
JTFName = new JTextField(10);
JTLName = new JTextField(10);
JTAge = new JTextField(10);
JTSearch = new JTextField(10);
//JPanel JP = new JPanel();
MyContain.add(JLFName);
MyContain.add(JTFName);
MyContain.add(JLLName);
MyContain.add(JTLName);
MyContain.add(JLAge);
MyContain.add(JTAge);
MyContain.add(NxtBtn);
MyContain.add(PrvBtn);
MyContain.add(FstBtn);
MyContain.add(LstBtn);
MyContain.add(UpdBtn);
MyContain.add(DelBtn);
MyContain.add(AddBtn);
MyContain.add(SveBtn);
MyContain.add(SrcBtn);
MyContain.add(JTSearch);
  
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Frame();
BtnAction();
}
public static void Connect()
{
try
{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String Database = "jdbc:odbc:MyDB";
Con = DriverManager.getConnection(Database);
St = Con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from MyTable";
RS = St.executeQuery(sql);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
  
  
public static void main(String[] args)
{
Connect();
new exepro();
}
public void Frame()
{
try
{
RS.next();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex)
{
}
}
public void BtnAction()
{
System.out.println("Called");
NxtBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
  
try
{
if(RS.next())
{
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
else
{
RS.previous();
JOptionPane.showMessageDialog
(null, "No more records");
}
}
catch(Exception ex)
{
}
}
});
PrvBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
if(RS.previous())
{
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
else
{
RS.next();
JOptionPane.showMessageDialog
(null, "No more records");
}
}
catch(Exception ex)
{
}
}
});
Exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
try
{
System.exit(0);
}
catch(Exception ex)
{
}
}
});
About.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
try
{
JFrame frame=new JFrame("Welcome");
JOptionPane.showMessageDialog(frame, "Hello.");
}
catch(Exception ex)
{
}
}
});
LstBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
RS.last();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex)
{
}
}
});
FstBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
RS.first();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex)
{
}
}
});
UpdBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String FName = JTFName.getText();
String LName = JTLName.getText();
String Age = JTAge.getText();
try
{
RS.updateString("FName", FName);
RS.updateString("LName", LName);
RS.updateString("Age", Age);
RS.updateRow();
JOptionPane.showMessageDialog
(null, "Record Updated");
}
catch(Exception ex)
{
}
}
});
DelBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
RS.deleteRow();
St.close();
RS.close();
St = Con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from MyTable";
RS = St.executeQuery(sql);
RS.next();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex)
{
}
}
});
AddBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JTFName.setText(" ");
JTLName.setText(" ");
JTAge.setText(" ");
}
});
SveBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String FName = JTFName.getText();
String LName = JTLName.getText();
String Age = JTAge.getText();
try
{
RS.moveToInsertRow();
RS.updateString("FName", FName);
RS.updateString("LName", LName);
RS.updateString("Age", Age);
RS.insertRow();
St.close();
RS.close();
St = Con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from MyTable";
RS = St.executeQuery(sql);
RS.next();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex)
{
}
}
});
SrcBtn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
St1 = Con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql2 = "SELECT * FROM MyTable WHERE CATSEARCH(LName, JTSearch.getText(), 'order by 1 desc')> 0";
RS1 = St1.executeQuery(sql2);
JTFName.setText(RS1.getString("FName"));
JTLName.setText(RS1.getString("LName"));
JTAge.setText(RS1.getString("Age"));

}
catch(Exception ex)
{

}
}
});
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote