给定程序中的每件事都是正确的,但未建立与数据库的连接。可能的原因是什么?是否与驱动程序相关的问题。我想在没有 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) {
}
}
}