Home Java Example Java Swing How To Fetch Data From Database Into JTextArea

Related Tutorials


 
 

Share on Google+Share on Google+

How To Fetch Data From Database Into JTextArea

Advertisement
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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


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: January 3, 2013

Related Tutorials

Discuss: How To Fetch Data From Database Into JTextArea  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
Tharindu
September 21, 2013
java

How To Fetch Data From Database Into Jlable
Joe
May 30, 2014
DB table on text editor

Nice work here. How can one display the entire database table on textarea/editor to enable one print the table with its content values? Is it possible too to use netbeans palette for this? Thanks for assisting.
rhinot
May 20, 2014
thank you

thank you for helping me to solve my problem....
DMCA.com