In this section, you will learn how to navigate database values using up and down keys and displayed the fetched values into another frame.
In this section, you will learn how to navigate database values using up and down keys and displayed the fetched values into another frame.In this section, you will learn how to navigate database values using up and down keys and displayed the fetched values into another frame. For this purpose,we have created a form consisting of two textfields, two labels and a button in order to show the database values. On pressing the up key, the following code will fetch one by one record from the database and display it on the textfields and on pressing keyboard's down key, it will show you the previous record. The button displays the fetched values into another frame.
Here is the code:
import java.awt.*; import java.sql.*; import javax.swing.*; import java.awt.event.*; public class NavigateTextField extends JFrame { JLabel label1, label2; JTextField text1, text2; JPanel panel; JButton button; int count = 0; public NavigateTextField() { panel = new JPanel(new GridLayout(3, 2)); label1 = new JLabel("Name"); label2 = new JLabel("Address"); text1 = new JTextField(5); text2 = new JTextField(5); button = new JButton("Detail"); text1.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { if (evt.getKeyCode() == KeyEvent.VK_UP) { count++; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/register","root","root"); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("Select * from data"); String name = "", address = ""; for (int i = 0; i < count; i++) { if (rs.next()) { name = rs.getString("name"); address = rs.getString("address"); } text1.setText(name); text2.setText(address); } } catch (Exception ex) { } } else if (evt.getKeyCode() == KeyEvent.VK_DOWN) { count--; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/register","root","root"); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("Select * from data"); String name = "", address = ""; for (int i = 0; i < count; i++) { if (rs.next()) { name = rs.getString("name"); address = rs.getString("address"); } text1.setText(name); text2.setText(address); } } catch (Exception ex) { } } } }); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { String value1 = text1.getText(); String value2 = text2.getText(); JLabel l1 = new JLabel("Name= " + value1); JLabel l2 = new JLabel("Address= " + value2); JPanel p = new JPanel(); p.add(l1); p.add(l2); JFrame f = new JFrame(); f.add(p); f.pack(); f.setVisible(true); } }); panel.add(label1); panel.add(text1); panel.add(label2); panel.add(text2); panel.add(button); add(panel); setSize(400, 100); setVisible(true); } public static void main(String[] args) { NavigateTextField n = new NavigateTextField(); } }
Output:
Next Record:
Here you have learned how to navigate value of data in a Swing application.