Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9032667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:02:17+00:00 2026-06-16T08:02:17+00:00

I am beginner in Java Programming. How can i show my output from console

  • 0

I am beginner in Java Programming. How can i show my output from console to JTextArea. Here my code only show the output console. Anyone could tell or show me how can i do it.Thanks.

import java.sql.*;

public class Route
{

public void routeList() 
{

    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "YarraTram";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root"; 
    String password = "abc123";
    try 
    {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url+dbName,userName,password);

        PreparedStatement statement = conn.prepareStatement("Select rid,route from route");
        ResultSet result = statement.executeQuery();

        while(result.next())
        {
            System.out.println(result.getString(1)+" "+result.getString(2));

        } 

        conn.close();

    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
 }
} 

here is another file

 import java.awt.FlowLayout;
 import javax.swing.JFrame;
 import javax.swing.JButton;
 import javax.swing.JTextArea;

 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;


 public class GUI
 {
    public void createAndShowGUI()
 {

    JButton button2 = new JButton("Route");

    //create a frame
    JFrame frame = new JFrame("Yarra Tram Route Finder");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new FlowLayout());

    frame.add(button2);

button2.addActionListener(new ActionListener() 
    {

            public void actionPerformed(ActionEvent e)
            {

            //Execute when button is pressed
                    showNewFrame();
                    Route route = new Route();
                    route.routeList();
            }
           public void showNewFrame() 
            {
                JFrame frame = new JFrame("Yarra Tram Route Finder (Route)" );
                JTextArea textArea = new JTextArea();

                frame.add(textArea);
                frame.setSize(500,120);
                frame.setLocationRelativeTo( null );
                frame.setVisible( true );
                textArea.setEditable( false );

            }
    });
            frame.pack();
    frame.setSize(350,100);
    frame.setVisible(true); 
   }
}   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:02:19+00:00Added an answer on June 16, 2026 at 8:02 am

    Modified changes in your existing code. Check this

    public class GUI extends JFrame {
    
        JButton button2;
        JTextArea textArea;
    
        public GUI() {
            super("Yarra Tram Route Finder");
        }
    
        public void routeList() {
    
            Connection conn = null;
            String url = "jdbc:mysql://localhost:3306/";
            String dbName = "YarraTram";
            String driver = "com.mysql.jdbc.Driver";
            String userName = "root";
            String password = "abc123";
            try {
                Class.forName(driver).newInstance();
                conn = DriverManager.getConnection(url + dbName, userName, password);
    
                PreparedStatement statement = conn.prepareStatement("Select rid,route from route");
                ResultSet result = statement.executeQuery();
    
                StringBuilder strBuilder = new StringBuilder();
                while (result.next()) {
                    strBuilder.append(result.getString(1)).append(" ").append(result.getString(2));
                    strBuilder.append("\n");
    
                }
                textArea.setText(strBuilder.toString());
                conn.close();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void createAndShowGUI() {
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(new FlowLayout());
            button2 = new JButton("Route");
            textArea = new JTextArea(20, 20);
            add(textArea);
            add(button2);
            button2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    routeList();
                }
            });
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    GUI gui = new GUI();
                    gui.createAndShowGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a beginner in Java Programming. My code encrypts data extracted from a
I have this question in my beginner Java programming class and I can't find
OK guys, up till now (and since I'm a beginner) I was programming Java
I am a beginner to android. in java and other programming languages we get
I am a beginner when it comes to programming with Android/Java, and I am
Hello :) I am beginner in Java Swing and I can't google solution for
I'm a beginner in practical real-world java programming. I don't really understand these basic
I am a beginner in programming (learning Java). I am trying to write a
I'm a beginner in programming web apps with Java EE. How do you name
I'm a beginner in Java programming and would like to understand what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.