Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connecting to Excel thru JDBC

 
Farooq Ali
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys...
Can anyone guide to how to connect to an excel file through JDBC-ODBC bridge.
I want to know what is written in the sql query instead of the tables
is this query right
"SELECT fruits from SHEET$1" where fruit is a column is sheet1
I know there are are a lot of post on this question in javaranch
can any one send the urls to those discussions....
thanks a lot
Farooq
 
Mark Simms
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) create a DSN that points to the excel (xls) file
2) apply this coding pattern usnig Sam$ as the sheet name:
import java.sql.*;
class JavaExcel
{
JavaExcel()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc dbc:JavaExcel","","");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select Name,Marks from [Sam$]");
while (rs.next())
{
System.out.println(rs.getString("Name"));
System.out.println(rs.getInt("Marks"));
System.out.println("-------");
}
}
catch (Exception e) {e.printStackTrace();}
}
public static void main(String[] args)
{
new JavaExcel();
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic