Results 1 to 7 of 7
  1. #1
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default Printing database table to a javaFX textarea GUI

    Hello all,

    I have a project for class that I am trying to figure out. For this project I have to create a GUI which allows the user to enter a table name. The program then connects to a local database and prints the table to a text area on the GUI. I was able to get everything working as far as connecting to the database, but for the life of me I cannot figure out how to print the contents of the table to a text area. Any suggestions would be great!

    Thanks!

    Java Code:
    package exercise;
    
    import java.sql.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.application.Application;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextArea;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class Exercise extends Application {
    
        private TextArea taContents = new TextArea();
        private TextField tfTable = new TextField();
    
        @Override
        public void start(Stage primaryStage) throws SQLException {
            Label lblTable = new Label("Table Name");
            Button btShow = new Button("Show Contents");
    
            HBox hb0 = new HBox();
            VBox vb0 = new VBox();
    
            hb0.setSpacing(15);
            vb0.setSpacing(15);
            vb0.setAlignment(Pos.CENTER);
            taContents.setMaxWidth(340);
            taContents.setMaxHeight(190);
    
            hb0.getChildren().addAll(lblTable, tfTable, btShow);
            vb0.getChildren().addAll(hb0, taContents);
            
            btShow.setOnAction(e -> {
                try {
                    initializeDB(tfTable.getText());
                } catch (SQLException ex) {
                    Logger.getLogger(Exercise34_05.class.getName()).log(Level.SEVERE, null, ex);
                }
            });
    
            Scene scene = new Scene(vb0, 350, 230);
            primaryStage.setTitle("Exercise");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        //Connect to database
        public void initializeDB(String db) throws SQLException {
            Connection myConn = null;
            Statement myStmt = null;
            ResultSet myRs = null;
            
            //Stringbuilder to create string for database query
            StringBuilder sb = new StringBuilder();
            
            sb.append("select * from ");
            sb.append(db);
            
            System.out.println(sb.toString());
            
            String user = "user";
            String pass = "pass";
    
            try {
                myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/javabook", user, pass);
    
                myStmt = myConn.createStatement();
    
                myRs = myStmt.executeQuery(sb.toString());
                
                taContents.setText(myRs.toString());
    
                System.out.println("Successful Connection");
    
            } catch (Exception ex) {
                System.out.println("Table does not exist. Enter another table name.");
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: Printing database table to a javaFX textarea GUI

    You have to loop over the ResultSet and append each line of text to the existing text in the textarea. Another StringBuilder instance for the text could come in handy.
    Last edited by benji2505; 01-13-2017 at 05:50 AM.

  3. #3
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default Re: Printing database table to a javaFX textarea GUI

    Ahh so it may be helpful to create another string builder, loop through the result set which pulls data from the table and then write it to the new string builder?

    What would be a good way to format the output once the second stringbuilder is finished being appended to?

    I suppose I could use tableView also. I may look into that. It is not a requirement for me to use a text area.
    Last edited by hornet7288; 01-13-2017 at 06:09 AM.

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: Printing database table to a javaFX textarea GUI

    Correct. Step1: get your result set (you have that already). Step2: loop over the result set and append each line to the textarea.
    Your first StringBuilder instance is for the query. But you can use it also to append text parts and then just use the endresult. It might be easier.
    Layout and format depend on your application. Many possibilities.

  5. #5
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default Re: Printing database table to a javaFX textarea GUI

    It seems that when I append the result set to a string builder I am getting a memory address or something. I even tried the toString() method that is available with result set with no luck.

  6. #6
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default Re: Printing database table to a javaFX textarea GUI

    I ended up getting it working by using a JTable. Thanks for all of the help benji!

  7. #7
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: Printing database table to a javaFX textarea GUI

    JTable is not a JavaFX component.
    You have to loop through the result set and get a String for every line or column (depends on the result that you are reading out). Then you take each String and append it to the text that shall be displayed. Appending the read out String to an existing String might be easier with a new StringBuilder instance. Sorry to be fuzzy.
    This is an example for appending text to a TextArea.
    This is an example for getting Strings our of a ResultSet.

Similar Threads

  1. Newline after Add data to TextArea From DataBase
    By Ridha in forum AWT / Swing
    Replies: 9
    Last Post: 02-01-2013, 03:56 PM
  2. Adding a table of text to a TextArea
    By aortell24 in forum New To Java
    Replies: 13
    Last Post: 06-12-2012, 06:29 PM
  3. printing arraylists in an table
    By abhilasha in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 10-01-2011, 08:36 AM
  4. Printing a table with a for loop help
    By shroomiin in forum New To Java
    Replies: 2
    Last Post: 10-31-2009, 01:47 AM
  5. Printing a table with a for loop help
    By shroomiin in forum New To Java
    Replies: 1
    Last Post: 10-31-2009, 01:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •