日期:2014-05-16 浏览次数:20444 次
import java.io.*;
import java.sql.*;
public class MobileDatabase
{
public static void main(String[] args)
{
try{
//Load JDBC driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
/*这里的数据库的url一定要写正确,这是关键,其中DBQ可以绝对路径,也可以是相对路径,为了体现数据存储路径的/独立性,你可以将数据库copy到不同的位试一下*/
String dbUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\test.mdb";
Connection con = DriverManager.getConnection(dbUrl,"","");
Statement state = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
//执行SQL语句
String sql = "select * from address";
ResultSet rs = state.executeQuery(sql);
//打印测试
if( rs != null ){
while( rs.next() ){
System.out.println("\t" + rs.getString("name"));
}
rs.close();
con.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}
}//End class MobileDatabase
?