I wrote a quick example of how to use it which I thought I would share. I had to think about how to do it since it has been so long since I did such a thing. There are two examples of how to connect: one example is a "standard" JDBC connection, and the other takes advantage of the DataSource abstraction.
Note: I created an ODBC connection in Windows to the database called JDBCODBC for this example to work.
Here is the NetBeans code for the project: JavaJDBCODBC.zip
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | package bridge; import java.sql.*; import javax.naming.NamingException; /** * <p>Example application of how to use the JDBC-ODBC Bridge.</p> * <p><b>Note:</b> You must have configured an ODBC connection called * <b>JDBCODBC</b>.</p> * * @author John Yeary * @version 1.0 */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) throws ClassNotFoundException, SQLException, NamingException { if (args.length < 3 ) { System.out.println( "Usage: bridge.Main username password sql" ); System.out.println( "\t Example: bridge.Main scott tiger \"select * from users\"" ); System.exit(- 1 ); } // Standard JDBC Connection Example Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); java.util.Properties prop = new java.util.Properties(); prop.put( "user" , args[ 0 ]); prop.put( "password" , args[ 1 ]); String url = "jdbc:odbc:JDBCODBC" ; Connection con = DriverManager.getConnection(url, prop); Statement s = con.createStatement(); ResultSet rs = s.executeQuery(args[ 2 ]); ResultSetMetaData metaData = rs.getMetaData(); System.out.println( "===> MetaData <===" ); for ( int i = 1 ; i <= metaData.getColumnCount(); i++) { System.out.println(metaData.getColumnName(i)); } System.out.println( "" ); rs.close(); s.close(); con.close(); // DataSource Example sun.jdbc.odbc.ee.DataSource ds = new sun.jdbc.odbc.ee.DataSource(); ds.setUser(args[ 0 ]); ds.setPassword(args[ 1 ]); ds.setDatabaseName( "JDBCODBC" ); Connection conx = ds.getConnection(); s = conx.createStatement(); rs = s.executeQuery(args[ 2 ]); metaData = rs.getMetaData(); System.out.println( "===> DataSource <===" ); for ( int i = 1 ; i <= metaData.getColumnCount(); i++) { System.out.println(metaData.getColumnName(i)); } System.out.println( "" ); conx.close(); } } |
0 comments :
Post a Comment