Conn string for java with Ms-Excel:
[Link]("[Link]"); String myDB = "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=c:/java/scratch/jdbc_test.xls;" + "DriverID=22;READONLY=false"; Connection conn = [Link](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 [Link].*; import [Link].*; import [Link].*; import [Link].*; public class Connect extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { [Link]("text/html"); PrintWriter out = [Link](); Connection con=null; Statement st=null; PreparedStatement pst=null; ResultSet rs; try { [Link] ("[Link]"); con=[Link]
("jdbc:odbc:Std","",""); st=[Link](); rs=[Link]("select * from [ExcelSheetName$]"); while([Link]()) { [Link]([Link] (1)); [Link]([Link] (2)); [Link]([Link] (3)); } } catch(Exception e) { [Link]("Errr :"+e); } } }
2)
import [Link].*; import [Link].*; import [Link].*; import [Link].*; public class Connect extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { [Link]("text/html"); PrintWriter out = [Link](); Connection con=null; Statement st=null; PreparedStatement pst=null; ResultSet rs; try { [Link]
("[Link]"); con=[Link] ("jdbc:odbc:Std","",""); st=[Link](); rs=[Link]("select * from [ExcelSheetName$]"); while([Link]()) { [Link]([Link] (1)); [Link]([Link] (2)); [Link]([Link] (3)); } } catch(Exception e) { [Link]("Errr :"+e); } } }
3)
import import import import [Link].*; [Link].*; [Link].*; [Link].*;
public class EmployeeReader1 { public static void main(String[] args) { Connection connection = null; try { [Link] ("[Link]"); Connection con = DriverManager .getConnection ("jdbc:odbc:friends_exl"); Statement st = [Link] (); ResultSet rs = [Link]
("Select * from [Sheet1$]"); ResultSetMetaData rsmd = [Link](); int numberOfColumns = [Link](); [Link]("No of cols " + numberOfColumns); while ([Link]()) { for (int i = 1; i <= numberOfColumns; i++) { if (i > 1) [Link](", "); String columnValue = [Link](i); [Link] (columnValue); } [Link](""); } [Link](); [Link](); } catch (Exception ex) { [Link]("Exception: "); [Link]([Link]()); } } }
4)
import import import import [Link].*; [Link].*; [Link].*; [Link].*;
public class EmployeeReader1 { public static void main(String[] args) { Connection connection = null;
try { [Link] ("[Link]"); Connection con = [Link]("jdbc:odbc:gowtham"); Statement st = [Link] (); ResultSet rs = [Link] ("select * from [Sheet1$]"); while([Link]()) { [Link] ("USER ID:"+[Link]("ID")); } [Link](); [Link](); } catch (Exception ex) { [Link]("Exception: "); [Link]([Link]()); } } } Step 1: goto control panel and select the Administrative Tools and click the odbc driver. Step 2: Then click the add button and select the microsoft excel driver (".xls") Step 3: then give the dsn name. (gowtham). Step 4: then click the select workbook button and choose the excel file then it work well.