Wednesday, 11 December 2013

jdbc program for reading the data from Excel sheet

/*step1: create the data in excel sheet, and save (same folder).
here i created: id, ename sal
 click->start->control panel->select administrative tool and then Data Sources (ODBC), TO OPEN ODBC Data Source
 Administrator dialog box
see the ppt for navigation
 */


import java.sql.*;

public class GetExcelSheet {

public static void main(String args[])throws SQLException, ClassNotFoundException {

Connection con=prepareConnection();
Statement st=con.createStatement();
String query = "select * from [Sheet1$]";
ResultSet rs=st.executeQuery(query);

System.out.println ("id\t ename\tsal:");
while (rs.next()){
System.out.print (rs.getInt(1)+ "\t");
System.out.print(rs.getString("ename")+ "\t");
System.out.println (rs.getDouble("sal"));
}//while
}//main

public static Connection prepareConnection()throws SQLException, ClassNotFoundException {

String driverClassName="sun.jdbc.odbc.JdbcOdbcDriver";
String url="jdbc:odbc:rajdsn";
Class.forName(driverClassName);
return DriverManager.getConnection(url);
}//prepareConnection
}//class
/*
D:\jdbc\prog\wed>javac GetExcelSheet.java

D:\jdbc\prog\wed>set classpath=D:\softwares\MySQL\new\mysql-connector-java-5.1.1
8-bin.jar;.;

D:\jdbc\prog\wed>java GetExcelSheet
*/

No comments:

Post a Comment