java - 每当我尝试从我的 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);
}
}
解决方案
推荐阅读
- c# - Xamarin.Forms 从 ListView 中删除项目并将其添加到另一个
- amazon-web-services - 我如何明确拒绝 lambda:InvokeFunction,但 IAM 的受信任资源除外?
- python - 使用 Dask 返回变异的输入
- c# - 将数据保存到数据库时出现外键约束错误
- react-admin - Api 平台分页自定义 page_parameter_name
- javascript - React 应用程序编译成功并且可以工作,但是在浏览器中重新加载页面后,我得到一个 TypeError var.filter is not a function
- android - 尝试在空对象引用上调用虚拟方法 'androidx.navigation.NavGraph androidx.navigation.NavDestination.getParent()'
- django-rest-framework - Django Rest Framework ModelSerializer 根据查询参数获取字段数据
- javascript - 如何遍历这些数组对象?
- python-3.x - 我让用户输入了一个支票号码,我想接受一个 CR 作为“0”