简体   繁体   English

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

[英]JDBC connection for .accdb access database in java

Every thing is right in the given program but connection to database is not established. 给定程序中的每件事都是正确的,但未建立与数据库的连接。 What could be the possible reason?Is it driver related problem. 可能是什么原因?是驱动程序相关的问题。 I want to do this without DSN. 我想在没有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) {
        }
    }
}

Verify you have de correct access drivers in the machine (Access 2013 redistributables). 验证计算机中是否具有正确的访问驱动程序(Access 2013可再发行文件)。 It could be x86 or x64. 可能是x86或x64。 Jre/Jdk should be in concordance: x86 or x64 Jre / Jdk应该一致:x86或x64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM