javasql-serversql-server-2012jtextareacallable-statement

how to enter data in JTextArea by using procedures in java


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

import java.awt.event.*;
public class STAFFnew implements ActionListener
{   
    JFrame frame;
    JPanel panel;
    JTable dataTable;
    String Status[]={"Processed","Pending"};
    JLabel comLabel,staffLabel;
    JTextField complaintField;
    JButton detailButton,submitButton;
    JTextArea descriptionArea,remarksArea;
    JScrollPane dataScroll,descriptionScroll,remarksScroll;

    public STAFFnew() 
    {
        frame=new JFrame("Staff");
        panel=new JPanel();
        dataTable=new JTable(100,5);
        dataScroll=new JScrollPane(dataTable);
        comLabel=new JLabel("Enter Complaint No:");
        staffLabel=new JLabel("STAFF PORTAL");
        Font font=new Font("ARIAL BLACK",Font.BOLD,30);
        staffLabel.setFont(font);
        complaintField=new JTextField(10);
        detailButton=new JButton("DETAILS");
        submitButton=new JButton("SUBMIT");
        descriptionArea=new JTextArea("DESCRIPTION");
        remarksArea=new JTextArea("REMARKS");
        descriptionScroll=new JScrollPane(descriptionArea);
        remarksScroll=new JScrollPane(remarksArea);

        panel.setLayout(null);

        staffLabel.setBounds(160,20,600,25);
        dataScroll.setBounds(10,50,570,210);
        comLabel.setBounds(10,268,111,15);
        complaintField.setBounds(125,268,100,20);
        detailButton.setBounds(250,268,100,20);
        submitButton.setBounds(400,268,100,20);
        descriptionScroll.setBounds(10,290,280,168);
        remarksScroll.setBounds(300,290,280,168);

        dataTable.getTableHeader().getColumnModel().getColumn(0).setHeaderValue("Com NO");
        dataTable.getTableHeader().getColumnModel().getColumn(1).setHeaderValue("Department");
        dataTable.getTableHeader().getColumnModel().getColumn(2).setHeaderValue("Title");
        dataTable.getTableHeader().getColumnModel().getColumn(3).setHeaderValue("Status");
        dataTable.getTableHeader().getColumnModel().getColumn(4).setHeaderValue("Date/Time");



        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:StudentDSN");
            CallableStatement cl=con.prepareCall("{call showStaffPortal}"); 

            ResultSet rs=cl.executeQuery();
            int i=0;

            while(rs.next())
            {

 //like this i have to add text in JTextArea

                dataTable.setValueAt(rs.getString(1),i,0);
                dataTable.setValueAt(rs.getString(2),i,1);
                dataTable.setValueAt(rs.getString(3),i,2);
                dataTable.setValueAt(rs.getString(4),i,3);
                dataTable.setValueAt(rs.getString(5),i,4);

                i++;
            }
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(frame, e+"");

        }



        frame.add(panel);
        panel.add(comLabel);
        panel.add(staffLabel);
        panel.add(dataScroll);
        panel.add(complaintField);
        panel.add(detailButton);
        panel.add(submitButton);

        panel.add(descriptionScroll);
        panel.add(remarksScroll);

        frame.setSize(600,500);
        frame.setVisible(true);
    }
    public static void main(String[] args) 
    {
        new STAFFnew();
    }
    public void actionPerformed(ActionEvent evt)
    {
        if (evt.getSource()==detailButton)
        {
            try
            {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con=DriverManager.getConnection("jdbc:odbc:StudentDSN");
                CallableStatement cl=con.prepareCall("{call detailsStaffPortal}"); 

                ResultSet rs=cl.executeQuery();
                int i=0;

                while(rs.next())
                {
                    descriptionArea.setJTextArea(); 



//how to enter the data in JTextArea by calling procedure as i
//have done above for the JTable and i know that i didnt create the
//object for detail button of ActionListener type*/*

                   i++;
                }
            }
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(frame, e+"");

            }

        }

    }

}

Edit :source code of stored procedure

create proc detailsStaffPortal 
@complaintNo int 
as 
begin 
select remarks,description from complaintTable where complaintNo=@complaintNo 
end

Solution

  • String str1=rs.getString(1); String str2=rs.getString(2);

                    remarksArea.setText(str1);
                    descriptionArea.setText(str2);
                    descriptionScroll.add(descriptionArea);
                    remarksScroll.add(remarksArea);
    

    by using string we can store the data firsrt and then use setText