|
|
|
Common DatabasesMicrosoft Access certainly may not be your favorite database -- however it is a very common database product. My problem with many tutorials on JDBC is that they refer you to the vendor documentation for the JDBC driver. After many years, I still have not found the documentation on the drivers provided with the Java JDKs for the common Microsoft databases. A recent entry on the Denver Java User's group suggested that SUN was no longer distributing the old ODBC-JDBC bridge drivers. As the sample program below demonstrates, this is not so. Nevertheless, they did mention a very interesting JDBC Driver for MS Access at http://www.objectweb.org/rmijdbc/Access/access.html for no charge. If money is not object, you may wish to visit http://industry.java.sun.com/products/jdbc/drivers for a list of drivers. If you are using MS SQL Server, you may download Microsoft's JDBC Driver MS SQL Server at http://msdn.microsoft.com/downloads/default.asp?URL=/downloads/sample.asp?url=/MSDN-FILES/027/001/779/msdncompositedoc.xml. However, these are not necessary as the following program demonstrates. Getting Started with JDBCHere is a simple program to demonstrate that the default SUN odbc-jdbc bridge is still viable. Click here to download the source. 1 import java.sql.*;
2 import java.util.*;
3 import java.io.*;
4
5 /*
6 * Begin commands to execute this file using Java with CMD.EXE
7 * javac -g JDBC_MSAccess_Demo.java
8 * javaw JDBC_MSAccess_Demo "jdbc:odbc:DRIVER={SQL Server};Server=ANGEL;Database=pubs;User Id=sa;Password=;" "SELECT au_lname, au_fname, phone FROM AUTHORS"
9 * javaw JDBC_MSAccess_Demo "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=\Program Files\Microsoft Office\Office10\Samples\Northwind.mdb;DefaultDir=;UID=Admin;PWD=;" "SELECT FirstName, LastName, HomePhone, Extension FROM EMPLOYEES"
10 * del JDBC_MSAccess_Demo.class
11 * End commands to execute this file using Java with CMD.EXE
12 *
13 */
14
15
16 /**
17 * Summary description for JDBC_MSAccess_Demo.
18 */
19 public class JDBC_MSAccess_Demo
20 {
21 /** @attribute System.STAThread() */
22 public static void main(String[] args)
23 {
24 PrintStream out = System.out;
25 InputStream in = System.in;
26 out.println("begin");
27 java.sql.Connection cnn;
28
29 try{
30 String src = args.length>0?args[0]:"jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=\Program Files\Microsoft Office\Office10\Samples\Northwind.mdb;DefaultDir=;UID=Admin;PWD=;",
31 sql_stmnt = args.length>1?args[1]:"SELECT * from PRODUCTS",
32 class_name = args.length>2?args[2]:"sun.jdbc.odbc.JdbcOdbcDriver";
33 out.println("source = "+src);
34 out.println("sql = "+sql_stmnt);
35 out.println("class = "+ class_name);
36 Class.forName(class_name);
37 cnn = DriverManager.getConnection(src);
38
39 try{
40 java.sql.Statement stmt = cnn.createStatement();
41 java.sql.ResultSet results = stmt.executeQuery(sql_stmnt);
42 java.sql.ResultSetMetaData mdata = results.getMetaData();
43 int nColumns = mdata.getColumnCount();
44 for(int column = 1; column <= nColumns; column++){
45 out.print(mdata.getColumnName(column)+"\t");
46 }
47 out.println();
48 for(int column = 1; column <= nColumns; column++){
49 out.print("("+mdata.getColumnTypeName(column)+")\t");
50 }
51 out.println();
52 for(int column = 1; column <= nColumns; column++){
53 out.print("("+mdata.getColumnLabel(column)+")\t");
54 }
55 out.println();
56 while(results.next()){
57 for(int column = 1; column <= nColumns; column++){
58 out.print(results.getString(column)+"\t");
59 }
60 out.println();
61 }
62 }
63 catch(java.sql.SQLException e){out.println(e); }
64 }
65 catch(java.sql.SQLException e){out.println(e); }
66 catch(ClassNotFoundException e){ System.out.println(e); }
67 }
68 }
69
|
|
Send mail to
webmaster@SIGNITEK.com with
questions or comments about this web site.
|