[英]I can't connect to MS Access with Java Eclipse
我正在嘗試連接到MS Access數據庫,但是由於某種原因它沒有連接。 它給我一個錯誤,“找不到數據源名稱,並且未指定默認驅動程序”。 我已經連接了mysql,沒有任何問題。 這是我第一次嘗試連接到MSAccess。
這是我的代碼:
import java.sql.*;
import javax.swing.JOptionPane;
public class Database2 {
public String DBname = "comlab";
public static String host = "localhost";
public String Username = "";
public String Password = "";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
public Database2() {
}
public Connection conn;
public ResultSet rs = null;
public Statement st = null;
String dbconnect = "jdbc:";
public void connect() throws SQLException {
try {
if(conn==null){
this.conn = DriverManager.getConnection("jdbc:odbc:Database11");
this.st = this.conn.createStatement();}
else
this.st = this.conn.createStatement();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "The system could not connect to the database." + e, "Connection Error",
JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
public void connect1() throws SQLException {
try {
this.conn = DriverManager.getConnection("jdbc:odbc:Database11");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "The system could not connect to the database." + e, "Connection Error",
JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
public void close(Connection conn, Statement st, ResultSet rs) {
try {
if (rs != null)
this.rs.close();
if (st != null) {
}
if (conn != null)
this.conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void close(Statement st, ResultSet rs) {
close(null, st, rs);
}
public void close(Statement st){
close(null, st, null);
}
}
`
import java.sql.*;
public class UserLogin {
public static void main(String[] args)
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:\\bank.accdb";
Connection conn = DriverManager.getConnection(url, "username", "password");
System.out.println("Connection Succesfull");
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.