5

I want to connect to an excel sheet using jdbc or some other method but i do not want to specify DSN for the same using administrative tool. Is their someway to do it using code? If yes how ?

Thanks in advance

Abhishek Singh
  • 10,243
  • 22
  • 74
  • 108
  • There are many solutions to access excel from Java. Apache POI is something I use – Jayan Aug 12 '13 at 07:02
  • 1
    The File i am using is more than 15 MB. Using POI gives me out of memory error. Because POI trys to load than entire sheet into JVM at once – Abhishek Singh Aug 12 '13 at 07:29

3 Answers3

7

It is also possible to connect to a spreadsheet without using DSN, which provides a more flexible way within code to point JDBC at an Excel file of interest without the accesses to a client registry to define the required DSN. Without DSN, the db connection is created as following, please not the difference of constructed JDBC URL:

java.sql.DriverManager.getConnection( "jdbc:odbc:Driver={Microsoft Excel Driver
(*.xls)};DBQ=C:/Documents and Settings/myPath/Desktop/qa.xls");

Here DBQ defines the path to the target spreadsheet file (qa.xls). Both backslash and forward slash work well.

Source: Available source

Pandiyan Cool
  • 6,381
  • 8
  • 51
  • 87
1

What you are eluding to is a DSN less connection string. See http://support.microsoft.com/kb/165866 for details. Yet, I would opt for Apache POI as mentioned by Jayan 14.

Tarik
  • 10,810
  • 2
  • 26
  • 40
  • The File i am using is more than 15 MB. Using POI gives me out of memory error. Because POI trys to load than entire sheet into JVM at once – Abhishek Singh Aug 12 '13 at 07:31
  • 1
    15MB is peanuts. Change the VM settings to allow for more heap and/or stack space. – Tarik Aug 12 '13 at 07:36
  • The system i am developing is to be used by different users on different machine. I dont find it as correct approach – Abhishek Singh Aug 12 '13 at 07:44
0

try changing the driver name from Microsoft Excel Driver(*.xls) to Driver do Microsoft Excel(*.xls)

java.sql.DriverManager.getConnection("jdbc:odbc:Driver={Driver do Microsoft Excel(*.xls)};DBQ=C:/Documents and Settings/myPath/Desktop/qa.xls");

and if you want to update the excel file use the following connection string:

java.sql.DriverManager.getConnection("jdbc:odbc:Driver={Driver do Microsoft Excel(*.xls)};DBQ=C:/Documents and Settings/myPath/Desktop/qa.xls;ReadOnly=0");

Muhammad Bilal
  • 2,106
  • 1
  • 15
  • 24
Kirti
  • 1