Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Revising a table that copies information over to a database table. I am having issues making the View button work. Could you help me understand

Revising a table that copies information over to a database table.

I am having issues making the "View" button work. Could you help me understand how to make this work? All of the other buttons work except that one and I am having issues understanding where I am going wrong. Thanks!

Here are my instructions for this button:

Revise Listing 35.2, CopyFileToTable.java, to always copy the data from a text file named staff to staff table in the test database. The user needs to type the username INFO211 and password java2.

When the view button is clicked, all contents in staff table should be retrieved and displayed in the text area as in the following figure (formatting the information in the text area is not necessary)

Here is what I have thus far:

package copyfiletostaffdemo;

import java.io.File; import java.io.FileNotFoundException; import java.sql.*; import java.util.Scanner; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.PasswordField; import javafx.scene.control.SplitPane; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage;

public class CopyFileToStaffDemo extends Application { private Connection connection; private Statement statement; private ResultSet resultSet; private TextField tfFilename = new TextField(); private TextArea taFile = new TextArea(); private TextField tfUsername = new TextField(); private PasswordField pfPassword = new PasswordField(); private ComboBox cbURL = new ComboBox<>(); private ComboBox cbDriver = new ComboBox<>(); private Button btViewFile = new Button("View File"); private Button btView = new Button("View"); private Button btCopy = new Button("Copy"); private Label lblStatus = new Label(); @Override public void start(Stage primaryStage) { cbURL.getItems().addAll(FXCollections.observableArrayList ("jdbc:mysql://localhost/test")); cbURL.getSelectionModel().selectFirst(); cbDriver.getItems().addAll(FXCollections.observableArrayList ("com.mysql.jdbc.Driver")); cbDriver.getSelectionModel().selectFirst(); GridPane gridPane = new GridPane(); gridPane.add(new Label("JDBC Driver: "), 0, 0); //Creates label JDBC Drivers gridPane.add(new Label("Database URL: "), 0, 1); //Creates the label Database URL gridPane.add(new Label("Username: "), 0, 2); //Creates the label Username gridPane.add(new Label("Password: "), 0, 3); //Creates the label Password gridPane.add(cbURL, 1, 0); gridPane.add(cbDriver, 1, 1); gridPane.add(tfUsername, 1, 2); gridPane.add(pfPassword, 1, 3); HBox hBoxConnection = new HBox(10); //HBox for the connection hBoxConnection.getChildren().addAll(lblStatus, btView, btCopy); //Adds the lblstatus and the connect button hBoxConnection.setAlignment(Pos.CENTER_RIGHT); //Sets the alignment of hBox1 to the center right VBox vBoxConnection = new VBox(6); //VBox for the connection vBoxConnection.getChildren().addAll(new Label ("Target Database Table"), gridPane, hBoxConnection); //Creates a new label and adds the gridPane as well as hBox1 gridPane.setStyle("-fx-border-color:black;"); //Sets a black border around the gridpane BorderPane bpFileName = new BorderPane(); bpFileName.setLeft(new Label("File Name")); bpFileName.setCenter(tfFilename); bpFileName.setRight(btViewFile); BorderPane bpFileContent = new BorderPane(); bpFileContent.setTop(bpFileName); bpFileContent.setCenter(taFile); BorderPane bpFileSource = new BorderPane(); bpFileSource.setTop(new Label("Source Text File")); bpFileSource.setCenter(bpFileContent); SplitPane splitPane = new SplitPane(); splitPane.getItems().addAll(bpFileSource, vBoxConnection); Scene scene = new Scene(splitPane, 800, 400); primaryStage.setTitle("CopyFileToStaff"); primaryStage.setScene(scene); primaryStage.show(); btViewFile.setOnAction(e -> showFile()); //btView.setOnAction(e -> processFile()); btCopy.setOnAction(e -> { try { copyFile(); } catch (Exception ex) { lblStatus.setText(ex.toString()); } }); } private void showFile() { Scanner input = null; try { input = new Scanner(new File(tfFilename.getText().trim())); while (input.hasNext()) { taFile.appendText(input.nextLine() + ' '); } } catch (FileNotFoundException ex) { System.out.println("File not found: " + tfFilename.getText()); } finally { if (input != null) input.close(); processFileCommand(); //Not sure how to format this } } private void processFileCommand(String inputCommand) { //String sqlCommand = ""; try { statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(inputCommand); int columnCount = resultSet.getMetaData().getColumnCount(); String row = ""; for (int i = 1; i <= columnCount; i++) { row += resultSet.getMetaData().getColumnName(i) + "\t"; } //Insert ta? while (resultSet.next()) { row = ""; for (int i = 1; i <= columnCount; i++) { row += resultSet.getString(i) + "\t"; } } } catch (SQLException ex) { ex.printStackTrace(); } } private void copyFile() throws Exception { Class.forName(cbDriver.getSelectionModel().getSelectedItem().trim()); System.out.println("Driver loaded"); Connection connection = DriverManager.getConnection( cbURL.getSelectionModel().getSelectedItem().trim(), tfUsername.getText().trim(), String.valueOf(pfPassword.getText()).trim()); System.out.println("Database copied"); taFile.setText(""); } public static void main(String[] args) { launch(args); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

2nd Edition

0470624701, 978-0470624708

More Books

Students also viewed these Databases questions

Question

What has been your desire for leadership in CVS Health?

Answered: 1 week ago

Question

Question 5) Let n = N and Y Answered: 1 week ago

Answered: 1 week ago

Question

4. Devise an interview strategy from the interviewers point of view

Answered: 1 week ago

Question

5. Prepare for the role of interviewee

Answered: 1 week ago