java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException 错误JAVA

标签 java mysql database

我收到一个我不完全理解的错误,为什么会出现?
我有一个表单,我可以在其中将详细信息输入到相关的 JTextBox 中,这些文本框中的数据应该发送到我在本地设置的数据库中。

完整的错误信息

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 ')VALUES'asd','asd',",",",",",",'Home','Female','White  
British' at line 1

代码

conn = (Connection) SQLConnect.ConnectDb();
    //inserting all values to database
    String sql = "INSERT INTO w1283057.criminalrecords "
            + "(FName, MName, Sname, DOB, Address, HPhone, BPhone, MPhone "
            + "ResidentStatus, Sex, Race, IncidentLocation, Zone, PremiseType "
            + "DateRecorded, TimeRecorded, Weapons, CrimeOffences) "
            + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    try{
        //Declaring all variable that wiil be collected from JTextField
        pst = conn.prepareStatement(sql);
        //coverting JText to String
        String fname = FName.getText();
        String mname = MName.getText();
        String surname = Surname.getText();
        String dob = DOB.getText();
        String fullAddress = FullAddress.getText();
        String hPhone = HPhone.getText();
        String bPhone = BPhone.getText();
        String mPhone = MPhone.getText();
        String incidentLocation = IncidentLocation.getText();
        String incidentZone = IncidentZone.getText();
        String incidentPrType = IncidentPrType.getText();
        String incidentDate = IncidentDate.getText();
        String incidentTime = IncidentTime.getText();
        String incidentWeapons = IncidentWeapons.getText();
        String crimeOffences = CrimeOffences.getText();
        String radioText = "";

        //collecting input data and assign them to variables            
            pst.setString(1, fname);
            pst.setString(2, mname);
            pst.setString(3, surname);
            pst.setString(4, dob);
            pst.setString(5, fullAddress);
            pst.setString(6, hPhone);
            pst.setString(7, bPhone);
            pst.setString(8, mPhone);
            //radio buttons Resident Status
            if(RHome.isSelected())
            {
                radioText = RHome.getText();
                pst.setString(9, radioText);
            }
            else if(RForeign.isSelected())
            {
                radioText = RForeign.getText();
                pst.setString(9, radioText);
            }
            //radio buttons Sex
            if(Male.isSelected())
            {
                radioText = Male.getText();
                pst.setString(10, radioText);

            }
            else if(Female.isSelected())
            {
                radioText = Female.getText();
                pst.setString(10, radioText);
            }
            //radio button Race
            if(WhiteBritish.isSelected())
            {
                radioText = WhiteBritish.getText();
                pst.setString(11, radioText);

            }
            else if(WhiteOther.isSelected())
            {
                radioText = WhiteOther.getText();
                pst.setString(11, radioText);
            }
            else if(Black.isSelected())
            {
                radioText = Black.getText();
                pst.setString(11, radioText);
            }
            else if(Asian.isSelected())
            {
                radioText = Asian.getText();
                pst.setString(11, radioText);
            }
            else if(Indian.isSelected())
            {
                radioText = Indian.getText();
                pst.setString(11, radioText);
            }
            /////////////////////////////////////
            pst.setString(12, incidentLocation);
            pst.setString(13, incidentZone);
            pst.setString(14, incidentPrType);
            pst.setString(15, incidentDate);
            pst.setString(16, incidentTime);
            pst.setString(17, incidentWeapons);
            pst.setString(18, crimeOffences);
            pst.executeUpdate();
            System.out.println("records added sucessfully");

    }
    catch (SQLException a)
        {
            JOptionPane.showMessageDialog(null, a);
        }

enter image description here

最佳答案

您的值列表末尾有一个逗号,删除它,或用适当的值填充它。

+ "DateRecorded, TimeRecorded, Weapons, CrimeOffences,) " <---
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,)"; <----

关于java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException 错误JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14245832/

相关文章:

java - 如何在 Java 中更改字符串

java - 在 PostgreSQL 中存储 OffsetDateTime 时如何禁用到 UTC 的转换

java - 删除 $HOME/.metadata 文件夹后 Eclipse 无法启动

mysql - ZF2 + Doctrine Entity - 将查询作为带有日期时间对象的数组返回?

MySQL 跨多列搜索重复项

sql - PostgreSQL 不使用部分索引

java - jaxb嵌套列表元素返回null

sql - 如何在 MySQL 中执行 FULL OUTER JOIN?

database - Neo4j - 理解密码查询

Java Setters 和 Getters 将数据存入 Java 数据库