使用java将excel电子表格数据导入MYSQL数据库

Posted

技术标签:

【中文标题】使用java将excel电子表格数据导入MYSQL数据库【英文标题】:import excel spreadsheet data into MYSQL database using java 【发布时间】:2016-06-23 07:58:00 【问题描述】:
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.hssf.usermodel.HSSFSheet;

public class assessmentdatabase_3 
    public static void main(String[] args) throws Exception 

        try 

        Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "root",
                    "pass");
            con.setAutoCommit(false);
            PreparedStatement pstm = null;
            FileInputStream input = new FileInputStream("filepath");
            POIFSFileSystem fs = new POIFSFileSystem(input);
            HSSFWorkbook wb = new HSSFWorkbook(fs);

            HSSFSheet sheet = wb.getSheetAt(0);
            Row row;
            for (int i = 1; i <= sheet.getLastRowNum(); i++) 

                row = sheet.getRow(i);
                String Process = row.getCell(0).getStringCellValue();
                double Level = row.getCell(2).getNumericCellValue();
                double A = row.getCell(3).getNumericCellValue();
                double B = row.getCell(4).getNumericCellValue();
                double C = row.getCell(5).getNumericCellValue();


                String sql = "INSERT INTO description_process_level VALUES('" + Process + "','" + Level + "','" + A + "',`" + B + "`,`" + C +)";
                pstm = (PreparedStatement) con.prepareStatement(sql);
                pstm.execute();
                System.out.println("Import rows " + i);
            
            con.commit();
            pstm.close();
            con.close();
            input.close();
            System.out.println("Success import excel to mysql table");
         catch (IOException e) 
            e.printStackTrace();
        
    

我已经调试了整个代码,它一直编译到第 47 行,并在第 48 行显示错误。我想将此数据导入 mysql。

Exception in thread "main" 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 '61.166666666666664`,`75.25`,'61.166666666666664',`61.166666666666664`,`75.25`)' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    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:942)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192)
    at fibb.assessmentdatabase_3.main(assessmentdatabase_3.java:48)

【问题讨论】:

【参考方案1】:

用这个改变你的查询:

String sql = "INSERT INTO description_process_level (process, level, a, b, c) VALUES('" + Process + "'," + Level + "," + A + "," + B + "," + C +")";
    你最后错过了, Double 不需要使用 ',只用于 String

我建议使用:

String sql = "INSERT INTO description_process_level (process, level, a, b, c) VALUES(?, ?, ?, ?, ?)";
pstm = (PreparedStatement) con.prepareStatement(sql);

pstm.setString(1, Process);
pstm.setDouble(2, Level);
pstm.setDouble(3, A);
pstm.setDouble(4, B);
pstm.setDouble(5, C);

这里是一个如何使用准备好的语句的例子:

http://www.mkyong.com/jdbc/jdbc-preparestatement-example-select-list-of-the-records/

【讨论】:

我试过了,但现在它开始显示不同的错误,即线程“main”java.sql.SQLException 中的异常:参数索引超出范围(1 > 参数数量,即 0)。 是的,当然你的数据库中有多少个参数,你应该指定你想要插入的参数,像这样, String sql = "INSERT INTO description_process_level (p1, p2, p3, p4 , p5) 值(?, ?, ?, ?, ?)";你可以看到我的编辑 String sql = "INSERT INTO description_process_level (process,level,a,b,c) VALUES (Process,Level,A,B,C)"; pstm = (PreparedStatement) con.prepareStatement(sql); pstm.setString(1, 进程); pstm.setDouble(2, Level); pstm.setDouble(3, A); pstm.setDouble(4, B); pstm.setDouble(5, C);仍然会出现同样的错误 no in values just make VALUES(?, ?, ?, ?, ?) ,仅此而已 我编辑我的答案,如果您的数据库没有任何问题,这应该对您有用

以上是关于使用java将excel电子表格数据导入MYSQL数据库的主要内容,如果未能解决你的问题,请参考以下文章

将 Excel 电子表格数据导入现有的 sql 表?

将 Excel 电子表格导入 MS Access 数据库

用java怎么将excel表格数据导入到mysql数据库中

将 Excel 电子表格导入 phpMyAdmin

将数据从 excel 电子表格导入 django 模型

用java怎么将excel表格数据导入到mysql数据库中