Conn string for java with Ms-Excel

:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String myDB = "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=c:/java/scratch/jdbc_test.xls;" + "DriverID=22;READONLY=false"; Connection conn = DriverManager.getConnection(myDB, "", "");

Another method for connect db:

create DSN for Microsoft Excel(.xsl) in controlpanel/administrativetools/datasources after that write normal code for driver class and getting connection Object.

Example pgms: 1)
import java.util.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class Connect extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); Connection con=null; Statement st=null; PreparedStatement pst=null; ResultSet rs; try { Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection

forName . while(rs.getString (2)). st=con. PreparedStatement pst=null. IOException { response.util. out. HttpServletResponse response) throws ServletException.*.executeQuery("select * from [ExcelSheetName$]"). out.getString (1)). PrintWriter out = response. Connection con=null.println("Errr :"+e).println(rs. Statement st=null.*.http.createStatement().getWriter().("jdbc:odbc:Std"."".setContentType("text/html"). } } catch(Exception e) { System. rs=st. public class Connect extends HttpServlet { public void service(HttpServletRequest request.*.next()) { out.out.servlet.servlet.sql. import javax.println(rs. } } } 2) import java. import java.""). import javax. try { Class.*. ResultSet rs.println(rs.getString (3)).

next()) { out.executeQuery .util."").getConnection ("jdbc:odbc:Std".sql.odbc.net.*.*.createStatement (). try { Class.getString (1)).*.io. out. while(rs. } } } 3) import import import import java.jdbc. public class EmployeeReader1 { public static void main(String[] args) { Connection connection = null. } } catch(Exception e) { System.getConnection ("jdbc:odbc:friends_exl"). java.println(rs. ResultSet rs = st. out.println(rs.odbc. java.JdbcOdbcDriver").("sun.println("Errr :"+e).getString (3)). rs=st."".executeQuery("select * from [ExcelSheetName$]"). Statement st = con. Connection con = DriverManager .createStatement().println(rs.out.JdbcOdbcDriver"). st=con.forName ("sun.getString (2)).*.jdbc. java. con=DriverManager.

*.err.util.net. java. } rs.out. .print (columnValue). while (rs. java.close(). } System.out. i <= numberOfColumns.getMessage()). ResultSetMetaData rsmd = rs.println("No of cols " + numberOfColumns). i++) { if (i > 1) System.println("").*. st.out. System.println(ex. int numberOfColumns = rsmd. public class EmployeeReader1 { public static void main(String[] args) { Connection connection = null.getColumnCount().getString(i).print(".*.("Select * from [Sheet1$]"). } catch (Exception ex) { System.err.close().sql.getMetaData().*. System.io. java. System. ").out. String columnValue = rs.print("Exception: "). } } } 4) import import import import java.next()) { for (int i = 1.

(gowtham). } catch (Exception ex) { System.getString("ID")). ResultSet rs = st.xls") Step 3: then give the dsn name. Step 4: then click the select workbook button and choose the excel file then it work well.JdbcOdbcDriver"). } rs.getMessage()).forName ("sun.createStatement (). . System.close().odbc.getConnection("jdbc:odbc:gowtham"). Statement st = con.out.err. Connection con = DriverManager.jdbc. } } } Step 1: goto control panel and select the Administrative Tools and click the odbc driver.next()) { System.err. while(rs. st.executeQuery ("select * from [Sheet1$]").println(ex.print("Exception: ").println ("USER ID:"+rs. Step 2: Then click the add button and select the microsoft excel driver (".close().try { Class.