Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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);
}
image text in transcribed

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

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago