2 在数据库文件a1.mdb中建立一个表Table1;
3 为Table1添加一列,并插入至少一条记录;
4 一个完整的类,直接拿去运行就可以。
import java.sql.*
public class ConnectAccess {
public static void main(String args[]) throws Exception {
ConnectAccess ca=new ConnectAccess()
ca.ConnectAccessFile()
ca.ConnectAccessDataSource()
}
public void ConnectAccessFile() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
/**
* 直接连接access文件。
*/
String dbur1 = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)}DBQ=d://a1.mdb"
Connection conn = DriverManager.getConnection(dbur1, "username", "password")
Statement stmt = conn.createStatement()
ResultSet rs = stmt.executeQuery("select * from Table1")
while (rs.next()) {
System.out.println(rs.getString(1))
}
rs.close()
stmt.close()
conn.close()
}
public void ConnectAccessDataSource()throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
String dbur1 = "jdbc:odbc:dataS1"// 此为ODBC连接方式
Connection conn = DriverManager.getConnection(dbur1, "username", "password")
Statement stmt = conn.createStatement()
ResultSet rs = stmt.executeQuery("select * from Table1")
while (rs.next()) {
System.out.println(rs.getString(1))
}
rs.close()
stmt.close()
conn.close()
}
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)