Home Tutorial Java Swing Navigate database values using up and down key of keyboard

 
 

Navigate database values using up and down key of keyboard
Posted on: November 28, 2009 at 12:00 AM
Advertisement
In this section, you will learn how to navigate database values using up and down keys and displayed the fetched values into another frame.

Navigate database values using up and down key of keyboard

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:

Advertisement

Related Tags for Navigate database values using up and down key of keyboard:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 28, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com