废话不多说,直接看示例:
Access 连接数据库一般有两种方式,桥连接和直连,以下是直连的方式。
另桥连接参见: http://ilinux.javaeye.com/blog/180224
import java.sql.*;
import com.szl.xml.options.configOptions;
public class ConnectionDao
{
String strDriver = “sun.jdbc.odbc.JdbcOdbcDriver”;
// String filePath =
String strUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D://hist_2008.mdb";// 注意 Driver (*.mdb)之间的空格
String strUserName = "";
String strPassword = "";
String strSQL = "select * from his10";
Connection conn;
Statement stat;
ResultSet res;
public Connection getConnection()
{
try
{
Class.forName(strDriver);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
try
{
conn = DriverManager.getConnection(strUrl,strUserName,strPassword);
}
catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
public ResultSet getRes(String strsql)
{
try
{
if(conn!=null)
{
stat = conn.createStatement();
res = stat.executeQuery(strsql);
while(res.next())
{
System.out.println("value : "+res.getString("sn")+" "+res.getString("datetime"));
}
}
return res;
}
catch (SQLException e)
{
e.printStackTrace();
}
return null;
}
//测试类
public static void main(String[] args)
{
// ConnectionDao conn = new ConnectionDao();
// conn.getConnection();
// conn.getRes("select * from hist10");
}
}
//另外注意一点:日期查询,access中日期查询格式比较特殊。
格式如下;
SELECT * FROM HIST10 WHERE DATETIME = #2009-12-10 12:23:00#
其中日期字符串要用#号括起来