简体   繁体   中英

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.

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). It could be x86 or x64. Jre/Jdk should be in concordance: x86 or x64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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