资源简介
模仿百度的搜索功能,采用java,jsp和ajax实现
代码片段和文件信息
package book.suggest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SearchSuggest extends HttpServlet {
public void doPost(HttpServletRequest request HttpServletResponse response)
throws ServletException java.io.IOException {
String search = request.getParameter(“txtSearch“);//获得请求中cate的值
//定义查询数据库的SQL语句
String sql = “select title from suggest where title like ‘“+search+“%‘ order by title“;
Connection conn = null;//声明Connection对象
Statement stmt = null;//声明Statement对象
ResultSet rs = null;//声明ResultSet对象
Vector vData = new Vector();
//response.setContentType(“text/xml“);//设置返回数据类型为xml格式
java.io.PrintWriter out = response.getWriter();
try {
// 加载数据库驱动类
Class.forName(“com.mysql.jdbc.Driver“);
// 访问数据库的地址
String url = “jdbc:mysql://localhost:3306/test“;
//创建Connection对象
conn = DriverManager.getConnection(url “root“ “root“);
// 创建Statement对象
stmt = conn.createStatement();
// 执行SQL语句,返回记录集
rs = stmt.executeQuery(sql);
//定义AblumEO实体对象
while (rs.next())
{
vData.add(rs.getString(“title“));
}
StringBuffer buf = new StringBuffer();
for (int i=0;i {
String keyword = (String)vData.get(i);
buf.append(keyword+“\n“);
}
out.print(buf.toString());
// out.print(parasToxml(vData));//调用parasToxml()方法
} catch (Exception e) {
e.printStackTrace();
} finally {//最后关必记录集,Connection对象
try {
// this will close any associated ResultSets
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException sqle) {
}
}
}
public void doGet(HttpServletRequest request HttpServletResponse response)
throws ServletException java.io.IOException {
doPost(request response);
}
/*
public String parasToxml(Vector v) {// 该方法将数据转化成xml格式输出
StringBuffer buf = new StringBuffer();
buf.append(“l version=\“1.0\“ encoding=\“utf-8\“?>“);
buf.append(““);
for (int i = 0; i < v.size(); i++) {
AlbumEO album = (AlbumEO) v.get(i);
buf.append(“- “);
buf.append(““ + album.getAlbumName() + “ “);
buf.append(““ + album.getAlbumURL() + “ “);
buf.append(“ription>“ + album.getAlbumDescription()
+ “ ription>“);
buf.append(“ “);
}
buf.append(“ “);
return buf.toString();
}
*/
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 408 2009-12-18 17:36 Suggest\.classpath
文件 294 2011-05-30 17:18 Suggest\.myme
文件 1535 2011-05-31 08:29 Suggest\.project
文件 2684 2011-05-31 16:40 Suggest\WebRoot\ajax_search.js
文件 2157 2011-05-31 16:53 Suggest\WebRoot\db.sql
文件 1117 2011-05-31 16:33 Suggest\WebRoot\suggest.html
文件 650 2011-05-30 17:24 Suggest\WebRoot\WEB-INF\web.xm
文件 236956 2006-05-19 15:20 Suggest\WebRoot\WEB-INF\lib\mysql.jar
文件 3388 2011-05-31 15:26 Suggest\WebRoot\WEB-INF\classes\book\suggest\SearchSuggest.class
文件 39 2006-05-19 15:20 Suggest\WebRoot\me
文件 2844 2011-05-31 15:26 Suggest\src\book\suggest\SearchSuggest.java
文件 500 2011-05-31 08:29 Suggest\.settings\.jsdtscope
文件 49 2011-05-31 08:29 Suggest\.settings\org.eclipse.wst.jsdt.ui.superType.container
文件 6 2011-05-31 08:29 Suggest\.settings\org.eclipse.wst.jsdt.ui.superType.name
目录 0 2011-05-31 16:53 Suggest\WebRoot\WEB-INF\classes\book\suggest
目录 0 2011-05-31 16:53 Suggest\WebRoot\WEB-INF\classes\book
目录 0 2011-05-31 16:53 Suggest\WebRoot\WEB-INF\lib
目录 0 2011-05-31 16:53 Suggest\WebRoot\WEB-INF\classes
目录 0 2011-05-31 16:53 Suggest\src\book\suggest
目录 0 2011-05-31 16:53 Suggest\WebRoot\WEB-INF
目录 0 2011-05-31 16:53 Suggest\WebRoot\me
目录 0 2011-05-31 16:53 Suggest\src\book
目录 0 2011-05-31 16:53 Suggest\WebRoot
目录 0 2011-05-31 16:53 Suggest\src
目录 0 2011-05-31 16:53 Suggest\.settings
目录 0 2011-05-31 16:53 Suggest\.myeclipse
目录 0 2011-05-31 16:53 Suggest
----------- --------- ---------- ----- ----
252627 27
............此处省略0个文件信息
- 上一篇:麻将游戏Java源码
- 下一篇:Java心理测试小程序
评论
共有 条评论