How To Fetch Data From Database Into JTextArea

In this section we will read about how to get the data from database table into JTextArea.

How To Fetch Data From Database Into JTextArea

In this section we will read about how to get the data from database table into JTextArea.

javax.swing.JTextArea is a lightweight component which displays the plain-text in multi-line. To read detail about JTextArea click here.

Example

Here an example is being given which will demonstrate you about how to fetch data and displayed into the textarea in java. In this example I have created a class named DisplayDatabaseDataToJTextArea.java. This class implements the ActionListener interface and also implemented its method actionPerformed() where the getOperation() method will be called when a button action is listened. The getOperation() method I have defined for fetching the data from database and set the database table value to the JTextArea by using the method setText() of JTextComponent. Before this I have created a method createUI() to display the components on a Frame.

Source Code

DisplayDatabaseDataToJTextArea.java

import java.sql.*;
import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
public class  DisplayDatabaseDataToJTextArea implements ActionListener{
	JTextArea textarea=new JTextArea(5,20);
	JButton b=new JButton("Get Data");
	public void createUI()
	{
		JFrame f=new JFrame();
        f.setLayout(null);
        JLabel label=new JLabel("Database data : ");        
        b.addActionListener(this);
        
        label.setBounds(10,40,100,20);
        textarea.setBounds(120,40,150,60);
        b.setBounds(120,110,100,20);
        
        f.add(label);        
        f.add(textarea);
        f.add(b);
        f.setVisible(true);
        f.setSize(350,200);
	}
    public static void main(String[] args){
    	DisplayDatabaseDataToJTextArea dd = new DisplayDatabaseDataToJTextArea();
    	dd.createUI();
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		b = (JButton)e.getSource();		
		getOperation();
	}
	public void getOperation()
	{		
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection con = DriverManager.getConnection("jdbc:odbc:swing");
			String sql = "select textAreaData from data";
			PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();			
			while(rs.next())
            {           	   
				textarea.setText(rs.getString(1));           	
            }
			
			JOptionPane.showMessageDialog(null, "Retrieved data succesfully.","Record Retrieved",
					JOptionPane.INFORMATION_MESSAGE);
		}
		catch(Exception ex)
		{
			JOptionPane.showMessageDialog(null, ex.getMessage(),"Error",
					JOptionPane.ERROR_MESSAGE);
		}			
	}    
}

Output

When you will execute this example you will get the output as follows :

1. Data into the database table is as follows :

2. When this code will be executed by you a frame will be opened which will contains a label, textarea, and button as follows :

3. When you will click on the Get Data button then the data of the database table will be fetched and displayed into the textarea as follows :

Download Source Code

Ads