我正在尝试编写一个允许用户访问pc部件sql数据库的代码。我有一种感觉我的查询字符串在处理程序中起作用,但我不确定。这是我得到的主要错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你有一个 SQL语法错误;查看与您的手册相对应的手册 MySQL服务器版本,用于在'地址,名称附近使用正确的语法, 地址,'第1行
package application;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
public class Main extends Application {
//Set Variables for later use
String query = " ";
PreparedStatement preparedStatement;
TextArea tf = new TextArea ();
TextField position = new TextField();
@Override
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
VBox checkBoxes = new VBox(20);
//Set Up check Boxes
CheckBox checkState = new CheckBox("state");
CheckBox checkName = new CheckBox("name");
CheckBox checkAddress = new CheckBox("address");
CheckBox checkCity = new CheckBox("city");
CheckBox checkCustID = new CheckBox("customer ID");
CheckBox checkZip = new CheckBox("zip");
checkBoxes.setPadding(new Insets(10,10,10,10));
checkBoxes.getChildren().addAll ( checkState ,checkName ,
checkAddress , checkCity ,checkCustID, checkZip );
//Set up Button
HBox executeButton = new HBox(10);
Button btExecute = new Button("Execute Query");
executeButton.setAlignment(Pos.CENTER);
executeButton.setPadding(new Insets(15,15,15,15));
executeButton.getChildren().add(btExecute);
//Set up location box
HBox locationbox = new HBox(30);
locationbox.setPadding(new Insets(10,10,10,10));
locationbox.setAlignment(Pos.CENTER);
Label location = new Label("location");
position.setPrefColumnCount(20);
//Set up scene
locationbox.getChildren().addAll(location,position);
pane.setPadding(new Insets(10,10,10,10));
pane.setLeft(checkBoxes);
pane.setTop(locationbox);
pane.setBottom(executeButton);
pane.setRight(tf);
Scene scene = new Scene(pane);
primaryStage.setTitle("PcParts Finder");
primaryStage.setScene(scene);
primaryStage.show();
//Change the query string depending on what box was checked
EventHandler<ActionEvent> handler = e -> {
if (checkCustID.isSelected ( ) )
query = query + "custid, ";
if (checkName.isSelected ( ) )
query = query + "name, ";
if (checkAddress.isSelected ( ) )
query = query + "address, ";
if (checkCity.isSelected ( ) )
query = query + "city, ";
if (checkState.isSelected ( ) )
query = query + "state, ";
if (checkZip.isSelected ( ) )
query = query + "zip, ";
};
checkCustID.setOnAction(handler);
checkName.setOnAction(handler);
checkAddress.setOnAction(handler);
checkCity.setOnAction(handler);
checkState.setOnAction(handler);
checkZip.setOnAction(handler);
btExecute.setOnAction ( e -> {
try {
setUpConnection() ;
} catch (ClassNotFoundException | SQLException e1) {
e1.printStackTrace();
}
try {
display();
} catch (SQLException e1) {
e1.printStackTrace();
}
} ) ;
}
public void setUpConnection() throws SQLException, ClassNotFoundException {
//Set up connection with the server
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost/mysql","scott","tiger");
String queryString = query + position.getText();
preparedStatement = connection.prepareStatement(queryString);
}
private void display() throws SQLException {
ResultSet results = preparedStatement.executeQuery();
//make a string of all criteria that was searched
if (results.next()) {
String custid = results.getString("custid");
String name = results.getString("name");
String address = results.getString("address");
String city = results.getString("city");
String state = results.getString("state");
String zip = results.getString("zip");
String string = toString(custid) + toString(name) + toString(address) + toString(city) + toString(state) + toString(zip);
tf.setText(string);
}
}
public static String toString(String str){
String x = " ";
if(str != null) {
return str;
} else
return x;
}
public static void main(String[] args) {
launch(args);
}
}