我建了一个access数据库,已经和ODBC相连接,可是debug edit souce lookup path
public static void main(String args[]){
 Connection con;
 Statement sql; 
 ResultSet rs;
try{
 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 }
catch(ClassNotFoundException e){
System.out.println(""+e);
}
try{
 con=DriverManager.getConnection("jdbc:odbc:elvis","","");
sql=con.createStatement();
rs=sql.executeQuery("Select * FROM PITs");
while(rs.next()){
 String serialnumber = rs.getString(1); 
 String macadress = rs.getString(2); 
System.out.println("serialnumber:"+serialnumber); 
System.out.println("macadress:"+macadress);
   }
con.close();
}
catch(SQLException el){}
 }

解决方案 »

  1.   

    你连接ACCESS的地址是哪里?根本没看到.mdb或者.accdb文件的路径,难道你用的是传说中手工配置ODBC桥的方法?估计是吧,那个配置起来很繁琐,而且程序没有任何可移植性。给你一个连接Access的Demo吧,需要安装AccessDatabaseEngine2010,但不需要手工配置ODBC桥了
    代码如下
    连接的是E盘下的books.accdb文件(2007格式),里面的字段就bookname,bookauthor,description,insertTime四个,最后一个是时间日期,前三个是字符串
    public class Access {
    String strurl = "jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=E:\\books.accdb;";
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    public Access(){
    try {
    strurl = "jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=E:\\books.accdb;";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    //利用Properties尝试解决乱码问题
    //实验发现如果不使用properties指定编码格式为GBK,那么在解析和插入中文的时候都非常麻烦
    //解析的时候需要使用new String(rs.getBytes(),"gbk");才能正常显示中文

    Properties p=new Properties();
    p.put("charSet", "GBK");

    conn = DriverManager.getConnection(strurl,p);

    stmt = conn.createStatement();


    // String bname=new String("中文".getBytes(),"gbk");
    // String bauthor=new String("中文".getBytes(),"gbk");
    // String bdesc=new String("中文".getBytes(),"gbk");
    String bname="天龙八部";
    String bauthor="金庸";
    String bdesc="小说";
    //
    // String insertSQL="insert into books(bookname,bookauthor,description) values('中文书','中文作者','中文描述'"
    // +")";

    String insertSQL="insert into books(bookname,bookauthor,description,insertTime) " +
    "values('"+bname+"','"+bauthor+"','"+bdesc+"',now())";
    System.out.println(insertSQL);
    stmt.execute(insertSQL);

    rs = stmt.executeQuery("select * from books");
    while (rs.next()) {
    // System.out.println(rs.getString("简介"));
    // int id=Integer.parseInt(rs.getString("ID"));
    int id=rs.getInt("ID");
    // String bookname=rs.getString("bookname");
    // String bookname=new String(rs.getBytes("bookname"),"gbk");
    // String bookauthor=new String(rs.getBytes("bookauthor"),"gbk");
    // String description=new String(rs.getBytes("description"),"gbk");

    String bookname=rs.getString("bookname");
    String bookauthor=rs.getString("bookauthor");
    String description=rs.getString("description");
    java.util.Date time=rs.getDate("insertTime");

    // String bookname=new String(rs.getString("bookname").getBytes(),"gbk");
    System.out.println("id ::"+id);
    System.out.println("bookname ::"+bookname);
    System.out.println("bookauthor ::"+bookauthor);
    System.out.println("description ::"+description);
    System.out.println("insertTime ::"+time);
    }
    } catch (Exception e) {
    String info="";
    // try {
    // info = new String(e.toString().getBytes(),"gbk");
    // } catch (UnsupportedEncodingException e1) {
    // // TODO Auto-generated catch block
    // e1.printStackTrace();
    // }
    // try {
    // info=new String(info.getBytes("Latin1"),"gbk");
    // } catch (UnsupportedEncodingException e1) {
    // // TODO Auto-generated catch block
    // e1.printStackTrace();
    // }
    System.out.println(info);
    }finally{

    try {
    rs.close();
    stmt.close();
    conn.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    }


    public static void main(String args[]) {
    new Access();
    }
    }
      

  2.   

    我用一下的方法连接的。
    怎样才可以用ODBC连接ACCESS呢?
    2006-10-27 21:51:01|  分类: 电脑文库 |  标签: |字号大中小 订阅 
    1.下载iss 并配置好(在本机测试要把 internet 信息服务器->默认网站属性->ip设置为 127.0.0.1)
    2.启动Access软件,选择文件->新建,选数据库
      单击确定,起个名字保存。
    3.现在开始创建表和字段,双击使用设计器创建表,在数据类型和字段名中分别输入字段名和类型,关闭表窗口,保存,起个表名。
    4.数据库建好了,在控制面版->性能维护->管理工具->ODBC 数据源 双击打开 
      系统DNS->添加选 driver do microsoft Access(*.mdb) 数据库->选择 (选中刚才建数据库和表的路径)。
    5.在DW 中新建站点,建好以后 选择 窗口->数据库单击+ 选数据库名称DNS 找到刚才建的数据源 ,起个连接名称 如cnn,单击测试,如果成功就是连好了。
      

  3.   

    程序好像没有什么区别哦,我的电脑没有权限安装了。那位高手帮我来看看了。
    程序在debug时候出现edit source lookup path.
      

  4.   

    哎,你这就是绝大部分程序员都已经不用的手动设置ODBC桥的方式,仅仅是传说了你这个错误估计是ODBC桥的地址的问题,Java程序找不到ODBC的数据源,如果你这种情况估计很少人能回答你的问题,因为两点
    第一,这种方式基本没人用
    第二,可能是手动ODBC桥建立和命名的问题,别人很难知道。楼主保重,不能因为没有安装权限就……
      

  5.   

    我已经安装了access database engine2007,可是还是不行报同样的错误。那位高手帮我看看了。
    代码如下:
    public static void main(String args[]){
     Connection con = null;
     Statement stmt = null; 
     ResultSet rs = null;
     String strurl="jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb,*.accdb);DBQ=C:\\Documents and Setting\\LeungE\\My Documents\\macadss.accdb;";
    try{
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     }
    catch(ClassNotFoundException e){
    System.out.println(""+e);
    }
    try{
     con=DriverManager.getConnection("jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb,*.accdb);DBQ=C:\\Documents and Setting\\LeungE\\My Documents\\macadss.mdb;");
    stmt=con.createStatement();
    //rs=stmt.executeQuery("Select * FROM PITs");
    String srialnumber="jaf1234ghfd";
    String mcadress="oh:ko:mh:0i";
    String insertSQL="insert into PITs(serialnumber,macadress)"+"values('"+srialnumber+"','"+mcadress+"')";
    System.out.println(insertSQL);
    stmt.execute(insertSQL);
    rs = stmt.executeQuery("select * from PITs");
    while(rs.next()){
     String serialnumber = rs.getString("serialnumber"); 
     String macadress = rs.getString("macadress"); 
    System.out.println("serialnumber:"+serialnumber); 
    System.out.println("macadress:"+macadress);
       }
    con.close();
    }
    catch(SQLException el){}
     }
      

  6.   

    gaoyong:
    i have already installed the access database engine2007.
    but i still have this same issue.
    Can you help me to deal with it?
      

  7.   

    please print the error message.
      

  8.   

    finished the classforname,next for connection
    started the connection with database
    Exception in thread "main" java.lang.NullPointerException
    at macadss.<init>(macadss.java:46)
    at macadss.main(macadss.java:57)public class macadss {
    Connection con;
     Statement stmt; 
     ResultSet rs;
     String strurl="jdbc:odbc:driver=Microsoft Access Driver (*.mdb);DBQ=C:\\macadress.mdb";
    public macadss(){
     
    try{
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     System.out.println("finished the classforname,next for connection");
     //}
    //catch(ClassNotFoundException e){
    //System.out.println(""+e);
    //}
    //try{
    System.out.println("started the connection with database");
    con = DriverManager.getConnection(strurl,"LeungE","Caoni.com");
    //con = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=C:\\macadress.accdb","LeungE","Caoni.com");
    System.out.println("finished the connection with database");
    stmt=con.createStatement();
    //rs=stmt.executeQuery("Select * FROM PITs");
    String srialnumber="jaf1234ghfd";
    String mcadress="oh:ko:mh:0i";
    String insertSQL="insert into PITs(serialnumber,macadress)"+"values('"+srialnumber+"','"+mcadress+"')";
    System.out.println(insertSQL);
    stmt.execute(insertSQL);
    rs = stmt.executeQuery("select * from PITs");
    while(rs.next()){
     String serialnumber = rs.getString("serialnumber"); 
     String macadress = rs.getString("macadress"); 
    System.out.println("serialnumber:"+serialnumber); 
    System.out.println("macadress:"+macadress);
       }
    //con.close();
    }
    catch(Exception e){}
            finally{
                
                try {
                    rs.close();
                    stmt.close();
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }  }
    }public static void main(String args[]) {
        new macadss();
    }
      

  9.   

     错误信息是:finished the classforname,next for connection
    started the connection with database
    Exception in thread "main" java.lang.NullPointerException
    at macadss.<init>(macadss.java:46)
    at macadss.main(macadss.java:57)
    好像是这个getConnection函数没有联接到数据库。各位高手帮我看看了。
      

  10.   

    郁闷,最终还是用ODBC桥接的方法连接了数据库。
    苦苦折腾了好久。
    方法就是上面的方法来连接ODBC的。
    供大家参考把。
    多谢大家了。
    import java.sql.Connection;
     2import java.sql.DriverManager;
     3import java.sql.ResultSet;
     4import java.sql.SQLException;
     5import java.sql.Statement;
     6
     7public class AccessTest {
     8    
     9    public static void main(String[] args) {
    10        Connection conn;
    11        Statement stmt;
    12        ResultSet rs;
    13        try {
    14            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    15        } catch (ClassNotFoundException e) {
    16            e.printStackTrace();
    17        }
    18        try {
    19            conn = DriverManager.getConnection("jdbc:odbc:finance", "", "");
    20            stmt = conn.createStatement();
    21            rs = stmt.executeQuery("select * from user");
    22            while (rs.next()) {
    23                String username = rs.getString(1); 
    24                String password = rs.getString(2);
    25                System.out.println("帐号:" + username);
    26                System.out.println("密码:" + password);
    27            }
    28            conn.close();
    29        } catch (SQLException el) {
    30            el.printStackTrace();
    31        }
    32    }
    33
    34}
    35