JavaFx应用程序中的SQL命令无效

JavaFx应用程序中的SQL命令无效,java,sql,Java,Sql,我想使用Java将数据从excel文件导入MySQL数据库。我使用for activity读取每一行并将其插入数据库。在第一次迭代之后,我得到了一个错误:无效的SQL命令。第一条记录被插入到数据库中。请帮我解决这个问题。下面是我的java代码。谢谢 public void importXLtoDB() { String sqlQuery = "INSERT INTO employee (dbSurname, dbName, dbBirthDate, dbEmail,

我想使用Java将数据从excel文件导入MySQL数据库。我使用for activity读取每一行并将其插入数据库。在第一次迭代之后,我得到了一个错误:无效的SQL命令。第一条记录被插入到数据库中。请帮我解决这个问题。下面是我的java代码。谢谢

public void importXLtoDB()  {
        String sqlQuery = "INSERT INTO employee (dbSurname, dbName, dbBirthDate, dbEmail, dbMobile, dbPabx, " +
                "dbDepartment, dbTeam, dbCurrentPosition, dbManager) VALUE (?,?,?,?,?,?,?,?,?,?)";
        try {


        connection = database.getConnection();
        pst = connection.prepareStatement(sqlQuery);

            FileChooser fileChooser = new FileChooser();
            fileChooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("Excel Files", "*.xlsx", "*.xls"));
            FileInputStream fileIn = new FileInputStream(fileChooser.showOpenDialog(null));
        XSSFWorkbook wb = new XSSFWorkbook(fileIn);
        XSSFSheet sheet = wb.getSheetAt(0);
        Row row;
        for (int i=1; i<= sheet.getLastRowNum(); i++) {
            row = sheet.getRow(i);
            java.util.Date utilDate = row.getCell(2).getDateCellValue();
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            pst.setString(1, row.getCell(0).getStringCellValue());
            pst.setString(2, row.getCell(1).getStringCellValue());
            pst.setDate(3, sqlDate);
            pst.setString(4, row.getCell(3).getStringCellValue());
            pst.setString(5, row.getCell(4).getStringCellValue());
            pst.setString(6, row.getCell(5).getStringCellValue());
            pst.setString(7, row.getCell(6).getStringCellValue());
            pst.setString(8, row.getCell(7).getStringCellValue());
            pst.setString(9, row.getCell(8).getStringCellValue());
            pst.setString(10, row.getCell(9).getStringCellValue());

            pst.execute();
        }
            NotificationType notificationType = NotificationType.INFORMATION;
            TrayNotification tray = new TrayNotification();
            tray.setTitle("Information Message");
            tray.setMessage("Employees Details Imported to Database");
            tray.setNotificationType(notificationType);
            tray.showAndDismiss(Duration.millis(3000));

            wb.close();
            fileIn.close();
            pst.close();
            rs.close();

    }catch (SQLException e) {
            NotificationType notificationType = NotificationType.ERROR;
            TrayNotification tray = new TrayNotification();
            tray.setTitle("Warning Message Dialog");
            tray.setMessage("Invalid SQL Command");
            tray.setNotificationType(notificationType);
            tray.showAndDismiss(Duration.millis(3000));
        }catch (IOException e) {
            NotificationType notificationType = NotificationType.ERROR;
            TrayNotification tray = new TrayNotification();
            tray.setTitle("Warning Message Dialog");
            tray.setMessage("The System cannot find de file");
            tray.setNotificationType(notificationType);
            tray.showAndDismiss(Duration.millis(3000));
        }
    }
public void importXLtoDB(){
String sqlQuery=“插入员工(dburname、dbName、dbBirthDate、dbEmail、dbMobile、dbPabx,”+
“dbDepartment、dbTeam、dbCurrentPosition、dbManager)值(?、、、、、、、、、、、、、、?)”;
试一试{
connection=database.getConnection();
pst=connection.prepareStatement(sqlQuery);
FileChooser FileChooser=newfilechooser();
fileChooser.getExtensionFilters().addAll(
新的FileChooser.ExtensionFilter(“Excel文件”,“*.xlsx”,“*.xls”);
FileInputStream fileIn=newfileinputstream(fileChooser.showOpenDialog(null));
XSSF工作簿wb=新XSSF工作簿(fileIn);
XSSFSheet-sheet=wb.getSheetAt(0);
行行;

对于(int i=1;i而言,正确的语法应为:

INSERT INTO employee (dbSurname, dbName, dbBirthDate, dbEmail, dbMobile, dbPabx, dbDepartment, dbTeam, dbCurrentPosition, dbManager) VALUES (?,?,?,?,?,?,?,?,?,?)";
它是
,而不是

此外,您应该将命令作为更新执行。因此,您应该将
pst.execute()
替换为
pst.executeQuery()

如果不起作用,请尝试将此代码块
pst=connection.prepareStatement(sqlQuery);
添加到for循环中

在for循环中应该是这样的:

    pst = connection.prepareStatement(sqlQuery);
    row = sheet.getRow(i);
    java.util.Date utilDate = row.getCell(2).getDateCellValue();
    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
    pst.setString(1, row.getCell(0).getStringCellValue());
    pst.setString(2, row.getCell(1).getStringCellValue());
    pst.setDate(3, sqlDate);
    pst.setString(4, row.getCell(3).getStringCellValue());
    pst.setString(5, row.getCell(4).getStringCellValue());
    pst.setString(6, row.getCell(5).getStringCellValue());
    pst.setString(7, row.getCell(6).getStringCellValue());
    pst.setString(8, row.getCell(7).getStringCellValue());
    pst.setString(9, row.getCell(8).getStringCellValue());
    pst.setString(10, row.getCell(9).getStringCellValue());

    pst.executeQuery();

我已经更改为值,并得到了相同的错误-无效的SQL Commnad,插入了第一条记录,其他人没有。那么这个问题与JavaFX有什么关系?这是一个纯Java问题。尝试使用
pst.executeQuery
,正如我在更新的答案中指出的那样。我更改为
pst.executeQuery
,得到了相同的错误,但现在第一条记录与fxSolved无关。我将MySQL数据库中的字符集更改为UTF8!