背景:使用要在Network Solutions託管的Apache服務器上運行的應用程序。朋友/客戶堅持使用Access數據庫而不是SQL數據庫。訪問(.mdb)使用jdbc進行數據庫操作
當前問題:寫了一個Java測試程序,以確保我可以連接到數據庫,然後再着手編寫整個後端。當我運行的Apache服務器的JVM這段代碼的最終產品將被託管:
import java.sql.*;
import java.io.*;
public class test {
public static void main(String[] args) {
try {
Class driverClass = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
DriverManager.registerDriver((Driver) driverClass.newInstance());
// set this to a MS Access DB you have on your machine
String filename = new File(".").getCanonicalPath() + "/ITEMS.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database+= filename.trim(); // add on to the end
// now we can get the connection from the DriverManager
System.out.println(database);
Connection conn = DriverManager.getConnection(database);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage() + " " + e.getLocalizedMessage());
e.printStackTrace();
}
}
}
我上線連接康恩= DriverManager.getConnection就一個空指針異常(數據庫)
這裏堆棧跟蹤:
java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.initialize(JdbcOdbcDriver.java:436)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:153)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at test.main(test.java:20)
爲了寫這個測試我用這個作爲我的主要來源:http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2691&lngWId=2
MS-Access將只能在Windows服務器上運行。 – 2012-03-29 00:50:57
MS-Access也是一個「桌面」數據庫 - 不是多用戶數據庫。如果您將訪問權限暴露給網絡,您將面臨一個受到傷害的世界。做一個大忙,並將所有數據移動到一個真正的多用戶數據庫,如Postgresql或MySql。 – GreyBeardedGeek 2012-03-29 01:46:57
@GreyBeardedGeek雖然我同意你的觀點,但是當別人付賬時,如果他們想要訪問,他們會獲得訪問權限,受到傷害等等。 – corsiKa 2013-09-19 15:52:42