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

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

Tutorials

  1. Display Image in Java
  2. Show Coordinates
  3. What is Java Swing?
  4. Creating a Frame
  5. Setting an Icon for a Frame in Java
  6. Show Dialog Box in Java
  7. Show message and confirm dialog box
  8. Show input dialog box
  9. Changing the Label of a JButton Component in Java
  10. Setting Multi-Line label on the Button
  11. Setting icon on the button in Java
  12. Making a Frame Non Resizable in Java
  13. Remove Minimize and Maximize Button of Frame in Java
  14. Removing the Title Bar of a Frame
  15. Setting Bounds for a maximized frame
  16. Iconifying and Maximizing a frame in Java
  17. Making a component drag gable in java
  18. Create a ToolBar in Java
  19. Animating Images in Java Application
  20. Drawing with Color in Java
  21. Drawing with Gradient Color in Java
  22. Adding a Rollover and Pressed Icon to a JButton Component in Java
  23. Creating Check Box in Java Swing
  24. Customize the Icon in a JCheckBox Component of Java Swing
  25. Create a JComboBox Component in Java
  26. Combo Box operation in Java Swing
  27. Create a JRadioButton Component in Java
  28. Selecting a Radio Button component in Java
  29. Create a JList Component in Java
  30. Setting Tool Tip Text for items in a JList Component
  31. Setting the Dimensions of an Item in a JList Component in Java
  32. Create a JSpinner Component in Java
  33. Show Time in JSpinner Component
  34. Disabling Keyboard Editing in a JSpinner Component
  35. Limiting the Values in a Number JSpinner Component
  36. JSlider Component of Java Swing
  37. Progress Bar in Java Swing
  38. Create menus and submenus in Java
  39. Crate a Popup Menu in Java
  40. Create a Popup Menus with Nested Menus in Java