天天看點

Java連接配接其它電腦共享的mdb庫(access資料庫)

//擷取連接配接

private static Connection getConnectionmdb(){

String domainip="192.168.0.111"; //ip

String username="Administrator"; //共享電腦使用者名

String password="xxx";//共享電腦密碼

String url="smb://192.168.0.111/g/test/test.mdb";  //共享檔案位址

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(domainip, username, password);

Connection conn=null;

try {

SmbFile fp = new SmbFile(url,auth); 

fp.connect();

String strConnect ="jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ="+fp.getUncPath();  

conn=DriverManager.getConnection(strConnect,"","");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

return conn;

}

//查詢資料

private void queryUser(){

String sql="select * from oa2_user";

Connection conn=getConnectionmdb();

PreparedStatement pre=null;

ResultSet rs=null;

try {

pre=conn.prepareStatement(sql);

rs=pre.executeQuery();

while(rs.next()){

System.out.println("第一條記錄:"+rs.getString(1));

System.out.println("第二條記錄:"+rs.getString(2));

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

Tool.release(rs,pre,conn);

}

}

涉及到一個jar檔案:jcifs.jar