我的xls文件的数据,

id    |   name       |  address
----------------------------------
01    |   mani       |   us
02    |   saro       |  india


我正在尝试使用以下代码将myexceldata.xls导出到MySql的表CheckExcel

package Default;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

@SuppressWarnings("unused")
public class automateImport
{
    public static void main(String[] args)
    {
        DBase db = new DBase();
        Connection conn = db.connect(
    "jdbc:mysql://localhost:3306/RTS","root","");
//        db.importData(conn,args[0]);
        db.importData(conn, "/home/raptorjd4/Desktop/DBToMysql.xls");
    }

}

class DBase
{
    public DBase()
    {
    }

    public Connection connect(String db_connect_str,
  String db_userid, String db_password)
    {
        Connection conn;
        try
        {
            Class.forName(
    "com.mysql.jdbc.Driver").newInstance();

            conn = DriverManager.getConnection(db_connect_str,
    db_userid, db_password);

        }
        catch(Exception e)
        {
            e.printStackTrace();
            conn = null;
        }

        return conn;
    }

    public void importData(Connection conn,String filename)
    {
        Statement stmt;
        String query;

        try
        {
            stmt = conn.createStatement(
    ResultSet.TYPE_SCROLL_SENSITIVE,
    ResultSet.CONCUR_UPDATABLE);
            String filename1 ="/home/raptorjd4/Desktop/DBToMysql.xls";
            String tablename ="CheckExcel";


           query ="LOAD DATA LOCAL INFILE \"" + filename1 + "\" INTO TABLE " + tablename + " FIELDS TERMINATED BY ','";
            stmt.executeUpdate(query);
            System.out.println("success");

        }
        catch(Exception e)
        {
            e.printStackTrace();
            stmt = null;
        }
    }
};


但是我的结果是



我知道我在FIELDS TERMINATED BY中做错了,但是即使我尝试使用FIELDS TERMINATED BY '|'FIELDS TERMINATED BY '\t',也没有用!

可以请人帮我吗?

最佳答案

LOAD DATA INFILE用于导入CSV数据(或更准确地说,是文本文件中的数据)。 Excel文件不是CSV(不是文本)。

10-08 13:13