netbeans 8.2 的词法错误 运行 SQL
Lexical Error running SQL for netbeans 8.2
我在 NetBeans 中配置了 Java DB
。当 运行 我的应用程序时,我正在尝试使用 GUI 本身编辑数据库中的数据。
我有应用程序 运行,它在 JTable
中显示数据,但是当我尝试使用 INSERT
或 DELETE
编辑数据时,我总是得到这个例外:
java.sql.SQLSyntaxErrorException: Lexical error at line 1, column 13. Encountered: "`" (96), after : "".
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.Statement.executeUpdate(Unknown Source)
at my.contacteditor.MembershipManagementTable.executeSQLQuery(MembershipManagementTable.java:91)
at my.contacteditor.MembershipManagementTable.RemoveUserActionPerformed(MembershipManagementTable.java:276)
at my.contacteditor.MembershipManagementTable.access0(MembershipManagementTable.java:22)
at my.contacteditor.MembershipManagementTable.actionPerformed(MembershipManagementTable.java:184)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue.run(EventQueue.java:733)
at java.awt.EventQueue.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: org.apache.derby.client.am.SqlException: Lexical error at line 1, column 13. Encountered: "`" (96), after : "".
at org.apache.derby.client.am.Statement.completeSqlca(Unknown Source)
at org.apache.derby.client.am.Statement.completeExecuteImmediate(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parseEXCSQLIMMreply(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.readExecuteImmediate(Unknown Source)
at org.apache.derby.client.net.StatementReply.readExecuteImmediate(Unknown Source)
at org.apache.derby.client.net.NetStatement.readExecuteImmediate_(Unknown Source)
at org.apache.derby.client.am.Statement.readExecuteImmediate(Unknown Source)
at org.apache.derby.client.am.Statement.flowExecute(Unknown Source)
at org.apache.derby.client.am.Statement.executeUpdateX(Unknown Source)
... 41 more
这是我的代码
public class MembershipManagementTable extends javax.swing.JFrame {
/**
* Creates new form MembershipManagementTable
*/
public MembershipManagementTable() {
initComponents();
Show_Users_In_Table();
}
public Connection getConnection(){
Connection con;
try{
con = DriverManager.getConnection("jdbc:derby://localhost:1527/MembershipManagementDB", "khanq7","password");
return con;
}
catch (Exception e){
e.printStackTrace();
return null;
}
}
public ArrayList<User> getUsersList(){
ArrayList<User> usersList = new ArrayList<User>();
Connection connection = getConnection();
String query = "SELECT * FROM APP.MEMBERS1";
Statement st;
ResultSet rs;
try{
st = connection.createStatement();
rs = st.executeQuery(query);
User user;
while(rs.next()){
user = new User(rs.getInt("ID"),rs.getString("FIRSTNAME"),rs.getString("SECONDNAME"),rs.getString("DOB"),rs.getString("EMAIL"),rs.getString("PHONENUMBER"),rs.getString("MEMBERSHIPTYPE"));
usersList.add(user);
}
}
catch(Exception e){
e.printStackTrace();
}
return usersList;
}
//displaying data in table
public void Show_Users_In_Table(){
ArrayList<User> list = getUsersList();
DefaultTableModel model = (DefaultTableModel)memberTable.getModel();
Object[] row = new Object[7];
for(int i = 0; i < list.size(); i++)
{
row[0] = list.get(i).getId();
row[1] = list.get(i).getFirstName();
row[2] = list.get(i).getSecondName();
row[3] = list.get(i).getDoB();
row[4] = list.get(i).getEmail();
row[5] = list.get(i).getPhoneNumber();
row[6] = list.get(i).getMembershipType();
model.addRow(row);
}
}
public void executeSQLQuery(String query, String message){
Connection con = getConnection();
Statement st;
try{
st = con.createStatement();
if((st.executeUpdate(query)) == 1){
DefaultTableModel model = (DefaultTableModel)memberTable.getModel();
model.setRowCount(0);
Show_Users_In_Table();
JOptionPane.showMessageDialog(null,"Data " + message +" Successfully");
}
else{
JOptionPane.showMessageDialog(null,"Data Not" + message );
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
private void AddNewUserActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new Registration().setVisible(true);
}
private void memberTableMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
int i = memberTable.getSelectedRow();
TableModel model = memberTable.getModel();
MemberIDField.setText(model.getValueAt(i,0).toString());
}
private void RemoveUserActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String query = "DELETE FROM `MEMBERS1` WHERE ID = "+MemberIDField.getText();
executeSQLQuery(query, "Deleted");
}
DerbyDB 不使用反引号来转义对象名称(例如,mysql 就是这样)。只需删除它们,您就可以了:
String query = "DELETE FROM MEMBERS1 WHERE ID = "+MemberIDField.getText();
// `s removed here --------^--------^
我在 NetBeans 中配置了 Java DB
。当 运行 我的应用程序时,我正在尝试使用 GUI 本身编辑数据库中的数据。
我有应用程序 运行,它在 JTable
中显示数据,但是当我尝试使用 INSERT
或 DELETE
编辑数据时,我总是得到这个例外:
java.sql.SQLSyntaxErrorException: Lexical error at line 1, column 13. Encountered: "`" (96), after : "".
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.Statement.executeUpdate(Unknown Source)
at my.contacteditor.MembershipManagementTable.executeSQLQuery(MembershipManagementTable.java:91)
at my.contacteditor.MembershipManagementTable.RemoveUserActionPerformed(MembershipManagementTable.java:276)
at my.contacteditor.MembershipManagementTable.access0(MembershipManagementTable.java:22)
at my.contacteditor.MembershipManagementTable.actionPerformed(MembershipManagementTable.java:184)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue.run(EventQueue.java:733)
at java.awt.EventQueue.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: org.apache.derby.client.am.SqlException: Lexical error at line 1, column 13. Encountered: "`" (96), after : "".
at org.apache.derby.client.am.Statement.completeSqlca(Unknown Source)
at org.apache.derby.client.am.Statement.completeExecuteImmediate(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parseEXCSQLIMMreply(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.readExecuteImmediate(Unknown Source)
at org.apache.derby.client.net.StatementReply.readExecuteImmediate(Unknown Source)
at org.apache.derby.client.net.NetStatement.readExecuteImmediate_(Unknown Source)
at org.apache.derby.client.am.Statement.readExecuteImmediate(Unknown Source)
at org.apache.derby.client.am.Statement.flowExecute(Unknown Source)
at org.apache.derby.client.am.Statement.executeUpdateX(Unknown Source)
... 41 more
这是我的代码
public class MembershipManagementTable extends javax.swing.JFrame {
/**
* Creates new form MembershipManagementTable
*/
public MembershipManagementTable() {
initComponents();
Show_Users_In_Table();
}
public Connection getConnection(){
Connection con;
try{
con = DriverManager.getConnection("jdbc:derby://localhost:1527/MembershipManagementDB", "khanq7","password");
return con;
}
catch (Exception e){
e.printStackTrace();
return null;
}
}
public ArrayList<User> getUsersList(){
ArrayList<User> usersList = new ArrayList<User>();
Connection connection = getConnection();
String query = "SELECT * FROM APP.MEMBERS1";
Statement st;
ResultSet rs;
try{
st = connection.createStatement();
rs = st.executeQuery(query);
User user;
while(rs.next()){
user = new User(rs.getInt("ID"),rs.getString("FIRSTNAME"),rs.getString("SECONDNAME"),rs.getString("DOB"),rs.getString("EMAIL"),rs.getString("PHONENUMBER"),rs.getString("MEMBERSHIPTYPE"));
usersList.add(user);
}
}
catch(Exception e){
e.printStackTrace();
}
return usersList;
}
//displaying data in table
public void Show_Users_In_Table(){
ArrayList<User> list = getUsersList();
DefaultTableModel model = (DefaultTableModel)memberTable.getModel();
Object[] row = new Object[7];
for(int i = 0; i < list.size(); i++)
{
row[0] = list.get(i).getId();
row[1] = list.get(i).getFirstName();
row[2] = list.get(i).getSecondName();
row[3] = list.get(i).getDoB();
row[4] = list.get(i).getEmail();
row[5] = list.get(i).getPhoneNumber();
row[6] = list.get(i).getMembershipType();
model.addRow(row);
}
}
public void executeSQLQuery(String query, String message){
Connection con = getConnection();
Statement st;
try{
st = con.createStatement();
if((st.executeUpdate(query)) == 1){
DefaultTableModel model = (DefaultTableModel)memberTable.getModel();
model.setRowCount(0);
Show_Users_In_Table();
JOptionPane.showMessageDialog(null,"Data " + message +" Successfully");
}
else{
JOptionPane.showMessageDialog(null,"Data Not" + message );
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
private void AddNewUserActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new Registration().setVisible(true);
}
private void memberTableMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
int i = memberTable.getSelectedRow();
TableModel model = memberTable.getModel();
MemberIDField.setText(model.getValueAt(i,0).toString());
}
private void RemoveUserActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String query = "DELETE FROM `MEMBERS1` WHERE ID = "+MemberIDField.getText();
executeSQLQuery(query, "Deleted");
}
DerbyDB 不使用反引号来转义对象名称(例如,mysql 就是这样)。只需删除它们,您就可以了:
String query = "DELETE FROM MEMBERS1 WHERE ID = "+MemberIDField.getText();
// `s removed here --------^--------^