当前位置:   article > 正文

MyEclipse使用jsp连接Access数据库_myeclipse连接access数据库

myeclipse连接access数据库

这里针对使用外部tomcat,

Jdk版本:jdk1.6.0_13(此为myeclipse默认jdk)

 

  1. 在电脑上正确安装了Access64位后,用Access创建数据库

新建数据库bookForm,数据结构如图

 

 

  1. 保存文件,将后缀名改为.mdb,我的文件名为Book.mdb。

 

3、在电脑C盘中找到C:\Windows\System32中的odbcad32.exe。

 

4、在弹出的窗口中点击右边的添加

 

5、选择有.mdb后缀的Access Driver

 

6、给他一个名称,再点选择,把你创建的文件导入进去

 

7、点确定保存,会出现你创建的DSN

chaxunAccess.jsp

<%@ page contentType="text/html;charset=utf-8" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="FindBook"%>

<HTML>

<Body bgcolor=cyan><center>

 <form action="">

   输入查询内容:<Input type=text name="findContent" value="JSP">

       <Select name="condition" size=1>

          <Option Selected value="bookISBN">ISBN

          <Option value="bookName">书名

          <Option value="bookAuthor">作者

          <Option value="bookPublish">出版社

          <Option value="bookTime">出版时间

          <Option value="bookAbstract">内容摘要

       </Select> 

       <Br>

       <INPUT type="radio" name="findMethod" value="start">前方一致

       <INPUT type="radio" name="findMethod" value="end">后方一致

       <INPUT type="radio" name="findMethod" value="contains">包含

        <Input type=submit value="提交">

  </form>

<%

   String findContent = request.getParameter("findContent");

   String condition = request.getParameter("condition");

   String findMethod = request.getParameter("findMethod");

   if(findContent==null){

      findContent="";

   }

   if(condition==null){

      condition="";

   }

   if(findMethod==null){

      findMethod="";

   }

%>

 <BR>查询到的图书:

 <FindBook:FindBook dataSource="information"

                    tableName="bookForm"

                    findContent="<%=findContent%>"

                    condition="<%=condition%>"

                    findMethod="<%=findMethod%>"/>

  <BR><%=giveResult%>

</BODY>

</HTML>

FindBook.jsp

<%@ tag import="java.sql.*" %>

<%@ tag pageEncoding="utf-8" %>

<%@ attribute name="dataSource" required="true" %>

<%@ attribute name="tableName" required="true" %>

<%@ attribute name="findContent" required="true" %>

<%@ attribute name="condition" required="true" %>

<%@ attribute name="findMethod" required="true" %>

<%@ variable name-given="giveResult"

 variable-class="java.lang.StringBuffer" scope="AT_END" %>

<%  

    byte b[]=findContent.getBytes("iso-8859-1");

    findContent=new String(b);

    try{  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    }

    catch(ClassNotFoundException e){

         out.print(e);       

    }

    Connection con;

    Statement sql;

    ResultSet rs;

    StringBuffer queryResult=new StringBuffer();      //查询结果

    String uri="jdbc:odbc:"+dataSource;

    try{    con=DriverManager.getConnection(uri,"","");

            sql=con.createStatement();

            String s="";

            if(findMethod.equals("start"))

               s= "select * from "+tableName+" where "+condition+" Like'"+findContent+"%'";

            if(findMethod.equals("end"))

               s= "select * from "+tableName+" where "+condition+" Like'%"+findContent+"'";

            if(findMethod.equals("contains"))

               s= "select * from "+tableName+" where "+condition+" Like'%"+findContent+"%'";

            rs=sql.executeQuery(s);

            queryResult.append("<table border=1>");

            queryResult.append("<tr>");

            queryResult.append("<th>ISBN</td>");

            queryResult.append("<th>图书名称</td>");

            queryResult.append("<th>作者</td>");

            queryResult.append("<th>价格</td>");

            queryResult.append("<th>出版社</td>");

            queryResult.append("<th>出版时间</td>");

            queryResult.append("<th>摘要</td>");

            queryResult.append("</tr>");

            int 字段个数=7;

            while(rs.next()){

               queryResult.append("<tr>");

               String bookISBN="";   

               for(int k=1;k<=字段个数;k++) {

                  if(k==7){

                       String bookAbstract=rs.getString(k);

                       String abs="<textarea  rows=6 colums=10/>"+bookAbstract+"</textarea>";

                        queryResult.append("<td>"+abs+"</td>");

                  }

                  else {

                        queryResult.append("<td>"+rs.getString(k)+"</td>");

                  }

                }

            }

            queryResult.append("</table>");

            jspContext.setAttribute("giveResult",queryResult);

            con.close();

       }

       catch(SQLException exp){

            jspContext.setAttribute("giveResult",new StringBuffer("请给出查询条件"));

       }

            %>

 

 

 

如果一切都没问题,那多半是jdk出问题了,亲身经历jdk1.6.0_13没问题jdk1.6.0_14就编译不通过。吐了~~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/559189
推荐阅读
相关标签
  

闽ICP备14008679号