Results 1 to 5 of 5
- 09-24-2010, 11:08 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
How to read excel sheet using select statments r else any easy way
Hi
can anyone guide me how to read an excel sheet with an select statement, so that i can place the values in variable's and insert into a table in mysql database.
i am able to read the values with below code but i am unable to get those values in an order means the entire record/row in to diff variables so that i can insert in the table:
This below code is not working properly it is throwing error: sun.jdbc.odbc.JdbcOdbcConnection cannot be cast to com.mysql.jdbc.ConnectionJava Code:for(int i =0; i < sheet.getColumns(); i++){ for(int j=1; j < sheet.getRows(); j++){ Cell cell = sheet.getCell(i, j); System.out.println(cell.getContents()); } }
can anyone guide me how to resolve this issue, i am new to java and i have to use this code in a bean which will be invoked from a jsp.Java Code:public String readexel(String filename, Connection conn) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); System.out.println("" + filename); String url = "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)}; DBQ=" + filename; System.out.println("\n\n\n" + url); conn = (Connection) DriverManager.getConnection(url); System.out.println(""); stmt = (Statement) conn.createStatement(); String queryl = "select * from [Sheet1$]"; ResultSet rs = (ResultSet) stmt.executeQuery(queryl); while(rs.next()) { email = rs.getString(1)+""; firstname = rs.getString(2)+""; System.out.println(">>>eamil & firstname is>>>>"+email +""+firstname); } } catch (Exception e) { System.out.println("Exception is" + e); } return ""; }Last edited by sasi.tati; 09-24-2010 at 11:11 AM.
- 09-24-2010, 11:51 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you post the complete error message, stack trace?
- 09-24-2010, 12:07 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Use POI or JExcel to read (which looks like what ou're attempting int he first code snippet).
If it's an insert per row, then read each row, build an object representing that row, and pass that object to your DAO to do the insert.
Don't treat an Excel sheet as a database...
- 09-24-2010, 12:48 PM #4
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
Hi,
Only this error is coming in the NetBeans: java.lang.ClassCastException: sun.jdbc.odbc.JdbcOdbcConnection cannot be cast to com.mysql.jdbc.Connection
by using stack trace method this error is coming in the console:
[Ljava.lang.StackTraceElement;@83cc67
I am able to read the excel sheet in column wise but i am unable to read in row wise with the first code, but it will be possible in the second code but that thing is throwing exception.
- 09-24-2010, 01:06 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Both JExcel and POI use rows...not knowing what excel reading package you;re using I can't really help.
I'll say again, an Excel sheet is not a database.
If you insist, then you'll need to post the stack trace...that is this:
is not a good idea as you lose loads of info, do e.printStackTtrace() instead.Java Code:} catch (Exception e) { System.out.println("Exception is" + e); }
You;re also not closing your resources (conn, stmt, rs), which should be done in a finally block.
From the teeny amount of exception data you've given it looks like somewhere in your code you're using a mysql driver for you odbc driver? Anyway, if you print the stack trace you'll see exactly what's wrong.
Similar Threads
-
SQL Query resultset into Excel Sheet
By chandpuri in forum JDBCReplies: 8Last Post: 01-24-2011, 06:53 PM -
How to create excel sheet?
By kishan in forum Advanced JavaReplies: 3Last Post: 07-13-2010, 01:15 PM -
How to read EXCEL Sheet using XML
By raju.i in forum New To JavaReplies: 1Last Post: 04-19-2010, 12:39 AM -
Excel sheet opening in read-only mode
By swati.jyoti in forum Advanced JavaReplies: 2Last Post: 09-08-2009, 02:08 PM -
How to set the AutoFilter to Excel Sheet with Wither POI or JXL
By lnarayana_boga in forum Advanced JavaReplies: 0Last Post: 01-29-2008, 09:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks