一开始我学的是要在xp中配置好数据源,之后才能连接access数据库,由于需要,所以就不想用odbc数据源连接,
上网找了很多方法,试过之后都是不行的,找了很久,才发现一个方法可以连接。
1.首先做一个javabean,代码如下
解释一下这一句:String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\Program Files\\Apache Software Foundation\\tomcat 6.0\\webapps\\test5\\a.mdb";
这一句表示,我的a.mdb是放在C:\\Program Files\\Apache Software Foundation\\tomcat 6.0\\webapps\\test5\\目录之下的位置,
记得是要用“\\”双斜线,因为是转义字符
暂时我只能用绝对路径来设置数据源,试过很多次,都是没有办法用相对路径来设置数据源
package test;
import java.io.*;
import java.util.*;
import java.sql.*;
public class conaccess
{
public String con()
{
Connection conn = null;
String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\Program Files\\Apache Software Foundation\\tomcat 6.0\\webapps\\test5\\a.mdb";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection(strurl,"","");
}
catch (Exception e)
{
System.out.println("connection fail");
}
return "connection successed!";
}
}
import java.io.*;
import java.util.*;
import java.sql.*;
public class conaccess
{
public String con()
{
Connection conn = null;
String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\Program Files\\Apache Software Foundation\\tomcat 6.0\\webapps\\test5\\a.mdb";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection(strurl,"","");
}
catch (Exception e)
{
System.out.println("connection fail");
}
return "connection successed!";
}
}
2.做一个jsp页面调用这个javabean
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<%@ page import="test.conaccess" %>
<body>
<jsp:useBean id="pp" class="test.conaccess" scope="page"></jsp:useBean>
<% out.print(pp.con()); %>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<%@ page import="test.conaccess" %>
<body>
<jsp:useBean id="pp" class="test.conaccess" scope="page"></jsp:useBean>
<% out.print(pp.con()); %>
</body>
</html>