//获取连接
private static Connection getConnectionmdb(){
String domainip="192.168.0.111"; //ipString 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