.accdb的JDBC连接访问java中的数据库

时间:2013-03-18 16:15:21

标签: java jdbc

在给定程序中,每件事情都是正确的,但未建立与数据库的连接。 可能的原因是什么?是否与驾驶员有关。我想在没有DSN的情况下这样做。

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class FormAccess1 extends Frame implements ActionListener {

    private static ResultSet rs;
    Panel p1;
    TextField t1, t2;
    Button next;

    public FormAccess1() {
        super("Applicant Detail");
        setLayout(new GridLayout(5, 1));
        p1 = new Panel();
        t1 = new TextField(10);
        t2 = new TextField(10);
        add(p1);
        next = new Button("Next");
        p1.add(new Label("book-id"));
        p1.add(t1);
        p1.add(new Label("book-title"));
        p1.add(t2);
        p1.add(next);
        next.addActionListener(this);
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == next) {
            try {
                rs.next();
            } catch (Exception em) {
            }
            showRecord(rs);
        }
    }

    public void showRecord(ResultSet rs) {
        try {
            t1.setText(rs.getString(1));
            t2.setText(rs.getString(2));
        } catch (Exception ex) {
        }
    }

    public static void main(String arg[]) {
        FormAccess1 app = new FormAccess1();
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.accdb)};DBQ=MyDatabase.accdb;DriverID=01";
            Connection con = DriverManager.getConnection(database, "", "");
            Statement state = con.createStatement();
            System.out.println(rs.getString(1));
            System.out.println(rs.getString(2));
            rs.next();
            app.showRecord(rs);
        } catch (Exception ey) {
        }
    }
}

1 个答案:

答案 0 :(得分:0)

验证您是否在计算机中具有正确的访问驱动程序(Access 2013可再发行组件)。它可能是x86或x64。 Jre / Jdk应该是一致的:x86或x64