Lexical Error running SQL for netbeans 8.2

问题: I have Java DB configured in NetBeans. I am trying to edit the data that would be in the database using the GUI itself when running my application. I have the application...

问题:

I have Java DB configured in NetBeans. I am trying to edit the data that would be in the database using the GUI itself when running my application.

I have the application running, and it shows the data inside a JTable, but when I try to edit the data using INSERT or DELETE I always get this exception:

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.access$200(MembershipManagementTable.java:22)
at my.contacteditor.MembershipManagementTable$3.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.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.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$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.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

Here is my code

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");
}                                          

回答1:

DerbyDB doesn't use backticks to escape object names (like, e.g., does). Just remove them and you should be OK:

String query = "DELETE FROM MEMBERS1 WHERE ID = "+MemberIDField.getText();
// `s removed here --------^--------^
  • 发表于 2019-03-23 04:41
  • 阅读 ( 221 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除