我正在编写一个类赋值的程序,并且我一直收到sql语法错误。我有jar,似乎我的程序连接到数据库,并且也加载了JBDC驱动程序,但我得到这个com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException错误消息。我不能为我的生活找出代码有什么问题。
package application;
import java.sql.Connection;
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.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;
import javafx.stage.Stage;
public class Info extends Application {
private String query = " ";
private TextArea text = new TextArea();
private TextField where = new TextField();
private PreparedStatement statement;
@Override
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
HBox hboxTop = new HBox(10);
CheckBox check_ID = new CheckBox("customer ID");
CheckBox check_Name = new CheckBox("name");
CheckBox check_Address = new CheckBox("address");
CheckBox check_City = new CheckBox("city");
CheckBox check_State = new CheckBox("state");
CheckBox check_Zip = new CheckBox("zip");
hboxTop.setPadding(new Insets(20, 20, 20, 20));
hboxTop.getChildren().addAll(check_ID, check_Name, check_Address, check_City, check_State, check_Zip);
VBox vbox1 = new VBox(10);
Button btSubmit = new Button("Submit");
vbox1.setAlignment(Pos.CENTER);
vbox1.setPadding(new Insets(10, 10, 10, 10));
vbox1.getChildren().add(btSubmit);
HBox hbox2 = new HBox(10);
Label label = new Label("Type the where condition: ");
where.setPrefColumnCount(40);
hbox2.setPadding(new Insets(10, 10, 10, 10));
hbox2.getChildren().addAll(label, where);
pane.setPadding(new Insets(10, 10, 10, 10));
pane.setTop(hboxTop);
pane.setLeft(hbox2);
pane.setBottom(text);
pane.setRight(vbox1);
Scene scene = new Scene(pane);
primaryStage.setTitle("PC PARTS");
primaryStage.setScene(scene);
primaryStage.show();
EventHandler<ActionEvent> handler = e -> {
if (check_ID.isSelected())
query = query + "custid,";
if (check_Name.isSelected())
query = query + "name,";
if (check_Address.isSelected())
query = query + "address,";
if (check_City.isSelected())
query = query + "city,";
if (check_State.isSelected())
query = query + "state";
if (check_Zip.isSelected())
query = query + "zip,";
};
check_ID.setOnAction(handler);
check_Name.setOnAction(handler);
check_Address.setOnAction(handler);
check_City.setOnAction(handler);
check_State.setOnAction(handler);
check_Zip.setOnAction(handler);
btSubmit.setOnAction(e -> {
initializeDB();
display();
});
}
private void initializeDB(){
try{
//load the JDBC driver
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded");
//establish a connection
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/pcparts", "root", "sesame");
System.out.println("Database connected");
String query_Str = "select" + query + "where" + where.getText();
// create a statement
statement = connection.prepareStatement(query_Str);
}
catch (Exception ex){
ex.printStackTrace();
}
}
private void display(){
try {
ResultSet rset = statement.executeQuery();
if (rset.next()){
String custid = rset.getString("custid");
String name = rset.getString("name");
String address = rset.getString("address");
String city = rset.getString("city");
String state = rset.getString("state");
String zip = rset.getString("zip");
String finalString =
filter(custid) + filter(name) + filter(address) + filter(city) + filter(state) + filter(zip);
text.setText(finalString);
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
public static String filter(String str){
String x = "";
if(str != null){
return str;
}
else
return x;
}
public static void main(String[] args) {
launch(args);
}
}
以下是我想要检索的sql中的信息。
mysql> use pcparts;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_pcparts |
+-------------------+
| customers |
| orders |
| parts |
+-------------------+
3 rows in set (0.03 sec)
mysql> SHOW COLUMNS FROM customers;
+---------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+----------------+
| custid | int(10) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
| address | char(20) | NO | | NULL | |
| city | char(16) | NO | | NULL | |
| state | char(2) | NO | | NULL | |
| zip | char(5) | NO | | NULL | |
+---------+----------+------+-----+---------+----------------+
6 rows in set (0.05 sec)
mysql> SELECT * FROM customers;
+--------+-------------+--------------+------------+-------+-------+
| custid | name | address | city | state | zip |
+--------+-------------+--------------+------------+-------+-------+
| 1 | Smith, Tim | 12 Elm St | Tampa | FL | 34567 |
| 2 | Jones, Tom | 435 Oak Dr | Dunedin | FL | 33245 |
| 3 | Avery, Bill | 623 Ash Ave | Clearwater | FL | 33655 |
| 4 | Kerr, Debra | 1573 Yew Crt | Clearwater | FL | 33442 |
| 5 | Tater, Dick | 15 Elm St | Dunedin | FL | 33333 |
+--------+-------------+--------------+------------+-------+-------+
5 rows in set (0.00 sec)
这是eclipse中出现的错误消息
Driver Loaded
Database connected
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2470)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2617)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2550)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1962)
at application.Info.display(Info.java:109)
at application.Info.lambda$1(Info.java:87)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)