In this section you will learn how to navigate the Database data using Java Swing.
In this section you will learn how to navigate the Database data using Java Swing.In this java tutorial section, you will learn how to navigate the Database data using Java Swing. For this, we have created a form with four buttons to perform an action. Through these buttons, we have allowed the user to get the first , last , next and previous records.
Here is the code of NavigateData.java:
import java.sql.*; import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.*; import javax.swing.table.*; public class NavigateData extends JFrame{ JTextField text1,text2,text3,text4; JLabel label1,label2,label3,label4; int count=0; JButton nextRecord,previousRecord,firstRecord,lastRecord; static JPanel panel; public static void main(String []args)throws Exception{ NavigateData navigate=new NavigateData(); navigate.setSize(350,200); navigate.setVisible(true); navigate.add(panel); } public NavigateData(){ text1=new JTextField(); text2=new JTextField(); text3=new JTextField(); text4=new JTextField(); label1=new JLabel("Employee ID"); label2=new JLabel("Employee Name"); label3=new JLabel("Employee Address"); label4=new JLabel("Employee Salary"); nextRecord=new JButton("Next"); previousRecord=new JButton("Previous"); firstRecord=new JButton("First"); lastRecord=new JButton("Last"); panel=new JPanel(new GridLayout(6,2)); panel.add(label1); panel.add(text1); panel.add(label2); panel.add(text2); panel.add(label3); panel.add(text3); panel.add(label4); panel.add(text4); panel.add(nextRecord); panel.add(previousRecord); panel.add(firstRecord); panel.add(lastRecord); nextRecord.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ count++; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:access"); Statement st = con.createStatement(); ResultSet rs=st.executeQuery("Select * from Employee"); String id="",name="",address="",salary=""; for(int i=0;i<count;i++){ if(rs.next()){ id=Integer.toString(rs.getInt("emp_id")); name=rs.getString("emp_name"); address=rs.getString("emp_address"); salary=Integer.toString(rs.getInt("emp_salary")); } text1.setText(id); text2.setText(name); text3.setText(address); text4.setText(salary); } } catch(Exception ex){} } }); previousRecord.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ count--; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:access"); Statement st = con.createStatement(); ResultSet rs=st.executeQuery("Select * from Employee"); String id="",name="",address="",salary=""; for(int i=0;i<count;i++){ if(rs.next()){ id=Integer.toString(rs.getInt("emp_id")); name=rs.getString("emp_name"); address=rs.getString("emp_address"); salary=Integer.toString(rs.getInt("emp_salary")); } text1.setText(id); text2.setText(name); text3.setText(address); text4.setText(salary); } } catch(Exception ex){} } }); firstRecord.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:access"); Statement st = con.createStatement(); ResultSet rs=st.executeQuery("Select * from Employee"); String id="",name="",address="",salary=""; if(rs.next()){ id=Integer.toString(rs.getInt("emp_id")); name=rs.getString("emp_name"); address=rs.getString("emp_address"); salary=Integer.toString(rs.getInt("emp_salary")); } text1.setText(id); text2.setText(name); text3.setText(address); text4.setText(salary); } catch(Exception ex){} } }); lastRecord.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:access"); Statement st = con.createStatement(); ResultSet rs=st.executeQuery("SELECT TOP 1 emp_id, emp_name, emp_address,emp_salary from Employee ORDER BY emp_id DESC"); String id="",name="",address="",salary=""; while(rs.next()){ id=Integer.toString(rs.getInt("emp_id")); name=rs.getString("emp_name"); address=rs.getString("emp_address"); salary=Integer.toString(rs.getInt("emp_salary")); } text1.setText(id); text2.setText(name); text3.setText(address); text4.setText(salary); } catch(Exception ex){} } }); } } |