aspose file tools*
The moose likes JDBC and the fly likes How to read and write data with excel? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to read and write data with excel?" Watch "How to read and write data with excel?" New topic
Author

How to read and write data with excel?

calvin yao
Greenhorn

Joined: Jul 05, 2002
Posts: 10
Must use jdbc dbc?
Thanks a lot!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
JDBC-ODBC Bridges or jdbc itself is normaly used to access for databases.
When you want to access an excel file you should use
POI which is a jakarta framework to read/write excel
files. You can find it under
http://jakarta.apache.org/poi/
[ August 13, 2002: Message edited by: Oliver Refle ]
Mahesh swami
Greenhorn

Joined: Aug 13, 2002
Posts: 26
Hello,
Just go through this code
Create an excel file called budget
add a sheet to it and name the sheet as main
write the following data in excel
A B
1 45
1 67
Now compile and run the program

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
public class ExcelReaderWriter {
public static void main( String [] args ) {
Connection c = null;
Statement stmt = null;
try {
// Using the JDBC ODBC Bridge to connect
// to the Excel workbook.
// There needs to be a ODBC DSN created for
// the Excel workbook prior to running the code.
// This uses a DSN called budget.
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
c = DriverManager.getConnection( "jdbc dbc:budget", "", "" );
stmt = c.createStatement();
if (args.length == 0) {
// The worksheet name is the table name - "main" in
// this example. We need to use the dollar sign
// following the table name.
String query = "select * from [main$];";
ResultSet rs = stmt.executeQuery( query );
System.out.println( "Found the following billing information:" );
while( rs.next() )
{
System.out.print( rs.getString( "A" ) + "\t");
System.out.print( rs.getString( "B" ) + "\t");
System.out.print( rs.getString }
rs.close();
} }
} // end of main
} // end of ExcelReaderWriter class
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The advantage of POI/HSSF is that you can create your excel file in a more object oriented way. So
you don't call any selects or somthing you have a structure which represent a excel file
e.g. workbook contains worksheets and this contains cell Objects.
So in my opinion this is a better way to handle what you would like to do.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read and write data with excel?