用java连接access数据库

     直接连数据库的时候一定要注意Microsoft Access Driver (*.mdb)中Driver后面的空格,我今天调试的半天才发现这个问题,郁闷至死~~~~~~~~~~
import java.sql.*;
public class DBConnection
{
public static void main(String args[])
{
try{
//String strurl="jdbc:odbc:driver={Microsoft Access Driver(*.mdb)};DBQ=db.mdb";
String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=db.mdb";
//String strurl ="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=db.mdb";//此为NO-DSN方式
//String strurl="jdbc:odbc:test";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("before connection");
Connection conn=DriverManager.getConnection(strurl);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from test");
if(rs.next())
{
System.out.println(rs.getString("id"));
}
}catch(Exception e)
{
System.out.println(e);
}
}
}

posted @ 2008-01-05 10:20  玉米疯收  阅读(664)  评论(0编辑  收藏  举报