LISTING 1: Java Code for Reading Data from SQL Server 2000 package jdbcsql; import java.sql.*; import java.util.Properties; import java.io.*; /** * Title: JDBC Sample to Access SQL Server * Description: Accesses the FOR XML functionality of * SQL Server and builds the resultset. * Copyright: Copyright (c) 2001 * Company: Gone West Systems * @author Rich Rollman * @version 1.0 */ public class JDBCSQL { public static void main(java.lang.String[] args) { queryODBCBridge(); } private static void queryODBCBridge() { try { // Load the driver and establish a connection. Driver myDriver = (Driver) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:odbc:sqlnorthwind","sa",""); // Create and execute an SQL statement. Statement stmt = conn.createStatement(); stmt.execute("select * from Customers as Customer order by CompanyName for xml auto"); ResultSet rs = stmt.getResultSet(); // Open a buffered writer on a file for output. BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("ODBCResults.xml"))); bw.write(""); int c; // Display the SQL results. while(rs.next()) { InputStream is = rs.getBinaryStream(1); InputStreamReader isr = new InputStreamReader(is,"UnicodeLittle"); while ((c = isr.read()) != -1) { bw.write(c); sb.append((char) c); } } bw.write(""); bw.close(); // Release database resources. rs.close(); stmt.close(); conn.close(); } catch (ClassNotFoundException cnfe) { System.out.println("Unable to load Driver Class: " + cnfe.getMessage()); cnfe.printStackTrace(System.out); return; } catch (SQLException se) { // Inform user of any SQL errors. System.out.println("SQL Exception: " + se.getMessage()); se.printStackTrace(System.out); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(System.out); } } }