首页 > 解决方案 > 每当我尝试从我的 GUI 访问我的 sql 数据库时,我都会收到错误消息

问题描述

我正在尝试编写一个允许用户访问 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);
}
}

标签: javamysqlsql

解决方案


推荐阅读