Display Recordset in Textbox Control

Experts,

Below is the incomplete sample code in java to display mysql recordset to textbox. Is it possible to use the procedure "public void paint(Graphics g)" just display recordset in the textbox or Applet?

import java.awt.*;
import java.applet.Applet;
import java.sql.*;

public class Query {

    Connection con = null;

    public Query() {

        try {
            System.out.print("  Loading JDBC Driver\n");
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            this.con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
            System.out.print("Connected..\n");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public void performQuery() {

        Statement stmt      = null;
        ResultSet rset      = null;
        String queryString  = "select code, name from names ";

        try {

            System.out.print("   Creating Statement...\n");
            stmt = con.createStatement ();

            System.out.print("   Opening ResultsSet...\n");
            rset = stmt.executeQuery(queryString);

            int counter = 0;
           
            while (rset.next()) {
                System.out.println();
                System.out.println("  Row [" + ++counter + "]");
                System.out.println("  ---------------------");
                System.out.println("      Code             -> " + rset.getString(1));
                System.out.println("      Name             -> " + rset.getString(2));
            }

            System.out.println();
            System.out.print("  Closing ResultSet...\n");
            rset.close();

            System.out.print("  Closing Statement...\n");
            stmt.close();

        } catch (SQLException e) {

            e.printStackTrace();
        }
    }


    public void closeConnection() {
        try {
            System.out.print("   Closing Connection...\n");
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void paint(Graphics g)
    {
      ?
      ?
      ?
    }

    public static void main(String[] args)
            throws java.lang.InterruptedException {

        Query qe = new Query();
        qe.performQuery();
        qe.closeConnection();
    }
}

Thanks :)



marvelsoftAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>Is it possible to use the procedure "public void paint(Graphics g)" just display recordset in the textbox or Applet?

Yes, but the following would be in descending order of desirability IMO:

a. display in JTable
b. display in other text component
c. display in paint
marvelsoftAuthor Commented:
Yes but how can I insert that things in my codes herewith? Is there anything I forgot in the structure of that codes? :)
CEHJCommented:
Well, which one do you want to do?
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

marvelsoftAuthor Commented:
c. Display in paint :)
CEHJCommented:
That's the most difficult one as you need to position the Strings, which should probably be held in a List, at the correct coordinates in paint. You also need to ensure that method is called by the system. At the moment it won't be as you have not subclassed Applet, which you should do. Applet should not have a main method in your case as the system won't call it. Any initialization should go in an overridden public void init() method. Closing of db resources should be done in a finally block or your db may be left in an inconsistent and resource-consuming state
marvelsoftAuthor Commented:
Ok I understand because I also tried that already. But what I'm  going try is that to put value into textbox from mysql record. Is this possible?

This is my very very simple simple codes.

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

import java.sql.*;


public class hello extends Applet implements ActionListener
{
      TextField text1;
      Button button1;

      Connection con = null;

      public void init()
      {
            System.out.println("This is a test.");
            try
            {
                  String driver = "com.mysql.jdbc.Driver";
                  Class.forName(driver);
                  String url = "jdbc:mysql://localhost/test";
                  con = DriverManager.getConnection(url, "root", "" );
              } catch (ClassNotFoundException e) {
                        e.printStackTrace();
              } catch (SQLException e) {
                        e.printStackTrace();
              }

            text1 = new TextField(20);
            add(text1);
            text1.setText("");
            button1 = new Button("Click here");
            add(button1);
            button1.addActionListener(this);
      }

      public void actionPerformed(ActionEvent event)
      {
              Statement stmt      = null;
              ResultSet rset      = null;
              String queryString  = "select code, name from names";

            String msg = new String ("Records :");
            if(event.getSource() == button1);
            {
                  
                  text1.setText(msg);

                    try {
                             stmt = con.createStatement ();

                              rset = stmt.executeQuery(queryString);

                              int counter = 0;

                              //while (rset.next()) {
                              //}

                              rset.close();
                              stmt.close();

                  } catch (SQLException e) {
                             e.printStackTrace();
                    }
            }
      }
}
CEHJCommented:
You should use a TextArea, not TextField. The code is otherwise coming along OK.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.