[quote]绝对路径[/quote]
[quote]相对路径 在该类同包目录下建一文件夹放入db1.accdb[/quote]
[quote]配置ODBC 系统DSN Xtreme Sample Database 11.5为配置DSN的名称[/quote]
static class MyConnention{
public static Connection getConnection() throws Exception{
/**配置数据源后*/
// String URL="jdbc:odbc:<MS Access Database配置数据源名称>";
// Connection c=DriverManager.getConnection(URL,"","");
/**不配置数据源*/
//jdbc:odbc:DRIVER={Microsoft Access Driver<此处必须有空格 >(*.mdb,<注意此处必须有一空格>*.accdb)};DBQ=<数据库名称路径>[;pwd=password]
String url1="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=f:/data/db1.accdb;pwd=qing";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection c=DriverManager.getConnection(url1);
return c;
}
}
[quote]相对路径 在该类同包目录下建一文件夹放入db1.accdb[/quote]
static class MyConnention{
public static Connection getConnection() throws Exception{
/**不配置数据源*/
//jdbc:odbc:DRIVER={Microsoft Access Driver<此处必须有空格 >(*.mdb,<注意此处必须有一空格>*.accdb)};DBQ=<数据库名称路径>[;pwd=password]
Class theClass=MyConnention.class;
java.net.URL u= theClass.getResource("db/db1.accdb");
String url=u.getPath().substring(1,u.getPath().length()).replaceAll("%20", " ");
String url1="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+url+";pwd=qing";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection c=DriverManager.getConnection(url1);
return c;
}
}
public class StoreSample extends HttpServlet {
private static final long serialVersionUID = 1L;
static Connection theConn;
/**
* @see HttpServlet#HttpServlet()
*/
public StoreSample() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
theConn=StoreSample.MyConnention.getConnection();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
ResultSet rs;
Statement stmt;
String sql="select * from asspay";
try {
stmt=theConn.createStatement();
rs=stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString("type"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//response.sendRedirect("/storeRemote.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
static class MyConnention{
public static Connection getConnection() throws Exception{
/**配置数据源后*/
// String URL="jdbc:odbc:MS Access Database";
// Connection c=DriverManager.getConnection(URL,"","");
/**不配置数据源*/
//jdbc:odbc:DRIVER={Microsoft Access Driver<此处必须有空格 >(*.mdb,<注意此处必须有一空格>*.accdb)};DBQ=<数据库名称路径>[;pwd=password]
String url1="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=f:/data/db1.accdb;pwd=qing";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection c=DriverManager.getConnection(url1);
return c;
}
}
}
[quote]配置ODBC 系统DSN Xtreme Sample Database 11.5为配置DSN的名称[/quote]
public class CRSampleDataSourceBean {
private static ResultSet resultSet = null;
private static Connection connection = null;
private static String connectionURL = "jdbc:odbc:Xtreme Sample Database 11.5";
private static String databaseClass = "sun.jdbc.odbc.JdbcOdbcDriver";
private static String query = "select * from customer";
public static void main(String[] args) {
CreateDat cd=new CreateDat();
try {
ResultSet rs=cd.getResultSet();
while(rs.next()){
System.out.println(rs.getString("Customer Name"));
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
static class CreateDat{
public CreateDat() {
try {
// Ensure database class exists
Class.forName(databaseClass);
// Create a connection
connection = DriverManager.getConnection(connectionURL, "", "");
} catch (ClassNotFoundException ex) {
System.out
.println("Ensure that database driver class is installed.");
ex.printStackTrace();
} catch (SQLException ex) {
System.out.println("SQL Exception #" + ex.getErrorCode() + " : "
+ ex.getLocalizedMessage());
ex.printStackTrace();
}
}
/*
* Any function that returns a java.sql.ResultSet within the Java class can
* be used in Crystal Reports to return report data. The function can also
* be parameterized to 'feed' in specific values to the function when
* querying.
*/
public ResultSet getResultSet() throws SQLException {
// Create an SQL statement to execute
Statement statement = connection.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
// Execute the select statement to populate the ResultSet
resultSet = statement.executeQuery(query);
return resultSet;
}
}