Question: I am having issues with my Java and SQL code running together, I don't know if my formatting in my SQL code is correct either.

I am having issues with my Java and SQL code running together, I don't know if my formatting in my SQL code is correct either. my java code is: import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ProductTable extends Application {
private Connection connection;
private void initializeDBConnection(){
//MySQL database credentials
String url ="jdbc:mysql://localhost:3306/javabook";
String user = "scott";
String password = "tiger";
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException excep){
excep.printStackTrace();
}
}
@Override
public void start(Stage primaryStage){
//initialize database connection
initializeDBConnection();
//created CheckBoxs
CheckBox checkVin = new CheckBox("Vin");
CheckBox checkMake = new CheckBox("Make");
CheckBox checkModel = new CheckBox("Model");
CheckBox checkYear = new CheckBox("Year");
CheckBox checkColor = new CheckBox("Color");
CheckBox checkPrice = new CheckBox("Price");
TextField WhereClause = new TextField();
WhereClause.setPromptText("where");
TextArea textArea = new TextArea();
Button btnExecute = new Button("Execute Query");
btnExecute.setOnAction(event -> executeQuery(checkVin, checkMake, checkModel, checkYear, checkColor, checkPrice, WhereClause, textArea));
//GridPane layout
GridPane gridPane = new GridPane();
gridPane.setVgap(10);
gridPane.setHgap(10);
gridPane.setPadding(new Insets(10));
gridPane.add(checkVin,0,0);
gridPane.add(checkMake,1,0);
gridPane.add(checkModel,2,0);
gridPane.add(checkYear,0,1);
gridPane.add(checkColor,1,1);
gridPane.add(checkPrice,2,1);
gridPane.add(WhereClause,0,2,3,1);
gridPane.add(btnExecute,0,3);
gridPane.add(textArea,0,4,3,1);
//Scene layout
Scene scene = new Scene(gridPane);
primaryStage.setTitle("Select fields of Products Table to Display");
primaryStage.setScene(scene);
primaryStage.show();
}
private void executeQuery(CheckBox checkVin, CheckBox checkMake, CheckBox checkModel, CheckBox checkYear, CheckBox checkColor, CheckBox checkPrice, TextField WhereClause, TextArea textArea){
List fields = new ArrayList>();
if (checkVin.isSelected()) fields.add("Vin");
if (checkMake.isSelected()) fields.add("Make");
if (checkModel.isSelected()) fields.add("Model");
if (checkYear.isSelected()) fields.add("Year");
if (checkColor.isSelected()) fields.add("Color");
if (checkPrice.isSelected()) fields.add("Price");
String query = "SELECT "+ String.join(",", fields)+" FROM Products";
if (!WhereClause.getText().isEmpty()){
query +=" WHERE "+ WhereClause.getText();
}
try (Statement stmt = connection.createStatement();
ResultSet rset = stmt.executeQuery(query)){
StringBuilder sbuild = new StringBuilder();
while (rset.next()){
for (String field : fields){
sbuild.append(rset.getString(field)).append("\t");
}
sbuild.append("
");
}
textArea.setText(sbuild.toString());
} catch (SQLException excep){
excep.printStackTrace();
}
}
public static void main(String[] args){
launch(args);
}
 I am having issues with my Java and SQL code running

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!