赞
踩
这里针对使用外部tomcat,
Jdk版本:jdk1.6.0_13(此为myeclipse默认jdk)
新建数据库bookForm,数据结构如图
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就编译不通过。吐了~~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。