一种jdbc-odbc桥连接Access的方法, 在xp系统下不需连接驱动(jar文件)
2009-04-10 10:05
483 查看
package util;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
class Simpleselect {
public static void main (String args[]) {
// String url = "jdbc:odbc:my-dsn";
String file_name="src/util/my-dsn.mdb";
String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+file_name+";PWD=123"; //如果没有密码:PSD=123则可以省去;
String query = "SELECT * FROM emp";
try {
// Load the jdbc-odbc bridge driver
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
// Attempt to connect to a driver. Each one
// of the registered drivers will be loaded until
// one is found that can process this URL
Connection con = DriverManager.getConnection (
url, "", "");
// If we were unable to connect, an exception
// would have been thrown. So, if we get here,
// we are successfully connected to the URL
// Check for, and display and warnings generated
// by the connect.
checkForWarning (con.getWarnings ());
// Get the DatabaseMetaData object and display
// some information about the connection
DatabaseMetaData dma = con.getMetaData ();
System.out.println("/nConnected to " + dma.getURL());
System.out.println("Driver " +
dma.getDriverName());
System.out.println("Version " +
dma.getDriverVersion());
System.out.println("");
// Create a Statement object so we can submit
// SQL statements to the driver
Statement stmt = con.createStatement ();
// Submit a query, creating a ResultSet object
ResultSet rs = stmt.executeQuery (query);
// Display all columns and rows from the result set
dispResultSet (rs);
// Close the result set
rs.close();
// Close the statement
stmt.close();
// Close the connection
con.close();
}
catch (SQLException ex) {
// A SQLException was generated. Catch it and
// display the error information. Note that there
// could be multiple error objects chained
// together
System.out.println ("/n*** SQLException caught ***/n");
while (ex != null) {
System.out.println ("SQLState: " +
ex.getSQLState ());
System.out.println ("Message: " +
ex.getMessage ());
System.out.println ("Vendor: " +
ex.getErrorCode ());
ex = ex.getNextException ();
System.out.println ("");
}
}
catch (java.lang.Exception ex) {
// Got some other type of exception. Dump it.
ex.printStackTrace ();
}
}
//-------------------------------------------------------------------
// checkForWarning
// Checks for and displays warnings. Returns true if a warning
// existed
//-------------------------------------------------------------------
private static boolean checkForWarning (SQLWarning warn)
throws SQLException
{
boolean rc = false;
// If a SQLWarning object was given, display the
// warning messages. Note that there could be
// multiple warnings chained together
if (warn != null) {
System.out.println ("/n *** Warning ***/n");
rc = true;
while (warn != null) {
System.out.println ("SQLState: " +
warn.getSQLState ());
System.out.println ("Message: " +
warn.getMessage ());
System.out.println ("Vendor: " +
warn.getErrorCode ());
System.out.println ("");
warn = warn.getNextWarning ();
}
}
return rc;
}
//-------------------------------------------------------------------
// dispResultSet
// Displays all columns and rows in the given result set
//-------------------------------------------------------------------
private static void dispResultSet (ResultSet rs)
throws SQLException
{
int i;
// Get the ResultSetMetaData. This will be used for
// the column headings
ResultSetMetaData rsmd = rs.getMetaData ();
// Get the number of columns in the result set
int numCols = rsmd.getColumnCount ();
// Display column headings
for (i=1; i<=numCols; i++) {
if (i > 1) System.out.print(",");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");
// Display data, fetching until end of the result set
while (rs.next ()) {
// Loop through each column, getting the
// column data and displaying
for (i=1; i<=numCols; i++) {
if (i > 1) System.out.print(",");
System.out.print(rs.getString(i));
}
System.out.println("");
// Fetch the next result set row
}
}
}
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
class Simpleselect {
public static void main (String args[]) {
// String url = "jdbc:odbc:my-dsn";
String file_name="src/util/my-dsn.mdb";
String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+file_name+";PWD=123"; //如果没有密码:PSD=123则可以省去;
String query = "SELECT * FROM emp";
try {
// Load the jdbc-odbc bridge driver
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
// Attempt to connect to a driver. Each one
// of the registered drivers will be loaded until
// one is found that can process this URL
Connection con = DriverManager.getConnection (
url, "", "");
// If we were unable to connect, an exception
// would have been thrown. So, if we get here,
// we are successfully connected to the URL
// Check for, and display and warnings generated
// by the connect.
checkForWarning (con.getWarnings ());
// Get the DatabaseMetaData object and display
// some information about the connection
DatabaseMetaData dma = con.getMetaData ();
System.out.println("/nConnected to " + dma.getURL());
System.out.println("Driver " +
dma.getDriverName());
System.out.println("Version " +
dma.getDriverVersion());
System.out.println("");
// Create a Statement object so we can submit
// SQL statements to the driver
Statement stmt = con.createStatement ();
// Submit a query, creating a ResultSet object
ResultSet rs = stmt.executeQuery (query);
// Display all columns and rows from the result set
dispResultSet (rs);
// Close the result set
rs.close();
// Close the statement
stmt.close();
// Close the connection
con.close();
}
catch (SQLException ex) {
// A SQLException was generated. Catch it and
// display the error information. Note that there
// could be multiple error objects chained
// together
System.out.println ("/n*** SQLException caught ***/n");
while (ex != null) {
System.out.println ("SQLState: " +
ex.getSQLState ());
System.out.println ("Message: " +
ex.getMessage ());
System.out.println ("Vendor: " +
ex.getErrorCode ());
ex = ex.getNextException ();
System.out.println ("");
}
}
catch (java.lang.Exception ex) {
// Got some other type of exception. Dump it.
ex.printStackTrace ();
}
}
//-------------------------------------------------------------------
// checkForWarning
// Checks for and displays warnings. Returns true if a warning
// existed
//-------------------------------------------------------------------
private static boolean checkForWarning (SQLWarning warn)
throws SQLException
{
boolean rc = false;
// If a SQLWarning object was given, display the
// warning messages. Note that there could be
// multiple warnings chained together
if (warn != null) {
System.out.println ("/n *** Warning ***/n");
rc = true;
while (warn != null) {
System.out.println ("SQLState: " +
warn.getSQLState ());
System.out.println ("Message: " +
warn.getMessage ());
System.out.println ("Vendor: " +
warn.getErrorCode ());
System.out.println ("");
warn = warn.getNextWarning ();
}
}
return rc;
}
//-------------------------------------------------------------------
// dispResultSet
// Displays all columns and rows in the given result set
//-------------------------------------------------------------------
private static void dispResultSet (ResultSet rs)
throws SQLException
{
int i;
// Get the ResultSetMetaData. This will be used for
// the column headings
ResultSetMetaData rsmd = rs.getMetaData ();
// Get the number of columns in the result set
int numCols = rsmd.getColumnCount ();
// Display column headings
for (i=1; i<=numCols; i++) {
if (i > 1) System.out.print(",");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");
// Display data, fetching until end of the result set
while (rs.next ()) {
// Loop through each column, getting the
// column data and displaying
for (i=1; i<=numCols; i++) {
if (i > 1) System.out.print(",");
System.out.print(rs.getString(i));
}
System.out.println("");
// Fetch the next result set row
}
}
}
相关文章推荐
- 使用java web 在jsp文件及Class中连接MySQL和SQLsever 的驱动方法
- jsp/java代码中用jdbc驱动代替jdbc-odbc桥连接SQL Server数据库
- 仅在ACCESS窗体设计中,实现数据文件连接的方法
- 用Jdbc-odbc直接连接access文件
- Android开发JDBC连接mysql数据库导入驱动方法
- JDBC连接各种数据库驱动方法与连接SQLServer2000与MySQL范例
- JDBC连接不上数据库的一种解决方法
- Pentaho - JDBC驱动JAR文件分发目录
- 实例讲解连接sqlserver2000与2005 - sqljdbc.jar 驱动
- 安装jdbc连接数据库驱动,在MyEclipse中导入外部jar包
- JDBC连接各种数据库驱动方法与连接SQLServer2000与MySQL范例
- 实例讲解连接sqlserver2000与2005 - sqljdbc.jar 驱动
- win7下java用jdbc驱动来连接sql server的方法 (转载)
- JSP(4)在jsp中使用JDBC-ODBC桥连接数据库的方法和异常处理
- asp.net通过配置文件连接Access的方法
- 用Jdbc-odbc直接连接access文件
- Oracle数据库和Java的连接 需要添加Oracle自带的驱动\product\11.2.0\dbhome_1\jdbc\lib\jdbc6_g.jar文件
- jdbc链接mysql的第二个例子。使用properties文件存放连接信息。使用jar文件一个
- java使用ODBC连接Access(mdb)文件错误解决方法
- 用Jdbc-odbc直接连接access文件