SQL insert statement in java with Excel file

Dear all,

I wrote a program is as the follow:

import java.io.*;

import java.sql.*;

public class handleExcel

{

Connection con = null;

Statement stmnt = null;

public handleExcel()

{

String excel = "C:\\EGS\\app_files\\info_update_form_exported_data.xls";

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String str="jdbc:odbc:DRIVER=Microsoft Excel Driver (*.xls);DBQ=" + excel + ";";

String sql = "insert into [Sheet1$] (Name, Age, Test1, Test2, Test3) values ('mary','16','aa','bb','vv')";

con = DriverManager.getConnection(str, "", "");

stmnt = con.createStatement();

stmnt.executeUpdate(sql);

}

catch(Exception e)

{

System.out.println("con is error!!");

e.printStackTrace();

}

}

public static void main(String[] args)

{

handleExcel TestHpc = new handleExcel();

}

}

But when I run it, the error is as the follow:

java.sql.SQLException: [Microsoft][ODBC Excel Driver]

at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)

at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)

at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3111)

at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)

at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:288)

at hk.gov.edb.util.handleExcel.<init>(handleExcel.java:31)

at hk.gov.edb.util.handleExcel.main(handleExcel.java:97)

Please help me to solve this problem. Thank you so much for your help!

Regards,

kzyo

[1685 byte] By [kzyoa] at [2007-11-27 11:54:22]
# 1

Hi

You can use the jakarta POI api in order to read/write Excel file from java. Pure Java, no drivers nedeed.

I tested and ok.

Hope this helps

yomismoa at 2007-7-29 18:55:32 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...