2012-12-23 102 views
0

我是Java編程的初學者。我怎樣才能顯示我的輸出從控制檯JTextArea。這裏我的代碼只顯示輸出控制檯。任何人都可以告訴或告訴我我該怎麼做。謝謝。如何顯示從控制檯到JTextArea的輸出

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(); 
    } 
} 
} 

這裏是另一個文件

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); 
    } 
} 

回答

3

在現有代碼改變而改變。檢查此

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

我試過這個,但它顯示jdbc:mysql:// localhost:3306 /在textarea而不是從sql查詢。 – user1907971

+1

已更新'textArea.setText(strBuilder.toString());'現在檢查'StringBuilder'的現在爲 – vels4j

+0

+1。 – trashgod

相關問題