0% found this document useful (0 votes)
54 views

Conn String For Java With Ms-Excel

The document provides code examples for connecting to a Microsoft Excel file from Java using JDBC. It describes using the sun.jdbc.odbc.JdbcOdbcDriver class to connect and execute queries. It also mentions creating an ODBC data source for the Excel file as an alternative connection method. Code samples are given to connect, execute queries, and retrieve result sets to print data from an Excel sheet.

Uploaded by

Sramrajini Cool
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Conn String For Java With Ms-Excel

The document provides code examples for connecting to a Microsoft Excel file from Java using JDBC. It describes using the sun.jdbc.odbc.JdbcOdbcDriver class to connect and execute queries. It also mentions creating an ODBC data source for the Excel file as an alternative connection method. Code samples are given to connect, execute queries, and retrieve result sets to print data from an Excel sheet.

Uploaded by

Sramrajini Cool
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

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

("jdbc:odbc:Std","",""); st=con.createStatement(); rs=st.executeQuery("select * from [ExcelSheetName$]"); while(rs.next()) { out.println(rs.getString (1)); out.println(rs.getString (2)); out.println(rs.getString (3)); } } catch(Exception e) { System.out.println("Errr :"+e); } } }

2)
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 ("jdbc:odbc:Std","",""); st=con.createStatement(); rs=st.executeQuery("select * from [ExcelSheetName$]"); while(rs.next()) { out.println(rs.getString (1)); out.println(rs.getString (2)); out.println(rs.getString (3)); } } catch(Exception e) { System.out.println("Errr :"+e); } } }

3)
import import import import java.io.*; java.net.*; java.sql.*; java.util.*;

public class EmployeeReader1 { public static void main(String[] args) { Connection connection = null; try { Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager .getConnection ("jdbc:odbc:friends_exl"); Statement st = con.createStatement (); ResultSet rs = st.executeQuery

("Select * from [Sheet1$]"); ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); System.out.println("No of cols " + numberOfColumns); while (rs.next()) { for (int i = 1; i <= numberOfColumns; i++) { if (i > 1) System.out.print(", "); String columnValue = rs.getString(i); System.out.print (columnValue); } System.out.println(""); } rs.close(); st.close(); } catch (Exception ex) { System.err.print("Exception: "); System.err.println(ex.getMessage()); } } }

4)
import import import import java.io.*; java.net.*; java.sql.*; java.util.*;

public class EmployeeReader1 { public static void main(String[] args) { Connection connection = null;

try { Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:gowtham"); Statement st = con.createStatement (); ResultSet rs = st.executeQuery ("select * from [Sheet1$]"); while(rs.next()) { System.out.println ("USER ID:"+rs.getString("ID")); } rs.close(); st.close(); } catch (Exception ex) { System.err.print("Exception: "); System.err.println(ex.getMessage()); } } } 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.

You might also like