package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.sql.*;
public class AjaxZipCodesServlet extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException {
String responseString = null;
String zipCode = req.getParameter("zip");
if (zipCode != null) {
HashMap location = getCityState(zipCode);
responseString = JSONUtil.buildJSON(location, "location");
}
if (responseString != null) {
//res.setContentType("text/xml");
res.setContentType("application/json");
res.setHeader("Cache-Control", "no-cache");
res.getWriter( ).write(responseString);
}else {
res.setContentType("text/xml");
res.setHeader("Cache-Control", "no-cache");
res.getWriter( ).write("?");
}
}
private HashMap getCityState(String zipCode) {
Connection con = DatabaseConnector.getConnection( );
HashMap cityStateMap = new HashMap( );
cityStateMap.put("zip", zipCode);
String queryString = "";
String city="";
String state="";
try {
queryString = "SELECT CITY,STATE FROM ZIPCODES where ZIPCODE=" + zipCode + ";";
Statement select = con.createStatement( );
//cityStateMap.put("city", "city");
ResultSet result = select.executeQuery(queryString);
while (result.next( )) {
city = result.getString("CITY");
cityStateMap.put("city", city);
state = result.getString("state");
cityStateMap.put("state", state);
}
}
catch (Exception e) {
System.out.println("exception caught getting city/state:" + queryString + " " + e.getMessage( ));
}
finally {
if (con != null)
{
try { con.close( ); }
catch (SQLException e) { }
}
}
return cityStateMap;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Ajax-database.zip_ajax连接数据库
共46个文件
jar:13个
xml:9个
class:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 9 浏览量
2022-09-24
05:37:24
上传
评论
收藏 10.14MB ZIP 举报
温馨提示
ajax连接数据库,整个过程的示范连接,非常详细
资源推荐
资源详情
资源评论
收起资源包目录
Ajax-database.zip (46个子文件)
Ajax-database
build.xml 3KB
ajax.js 416B
src
conf
MANIFEST.MF 25B
java
servlet
AjaxZipCodesServlet.java 2KB
JSONUtil.java 824B
DatabaseConnector.java 889B
AjaxUsernameServlet.java 2KB
nbproject
build-impl.xml 80KB
private
private.xml 230B
private.properties 438B
ant-deploy.xml 3KB
project.xml 1KB
genfiles.properties 473B
project.properties 3KB
web
META-INF
context.xml 94B
example5.2.html 2KB
WEB-INF
web.xml 972B
ajax.js 2KB
index.html 2KB
lib
ezmorph-1.0.6.jar 84KB
mysql-connector-java-5.1.7-bin.jar 693KB
commons-beanutils-1.7.jar 184KB
commons-collections.jar 558KB
javaee-endorsed-api-6.0
javaee6-doc-api.zip 7.53MB
webservices-api-osgi.jar 50KB
jaxb-api-osgi.jar 101KB
javax.annotation.jar 21KB
commons-logging.jar 52KB
nblibraries.properties 585B
json-lib-2.3-jdk15.jar 148KB
json_simple-1.1.jar 16KB
CopyLibs
org-netbeans-modules-java-j2seproject-copylibstask.jar 22KB
commons-lang.jar 237KB
build
empty
web
META-INF
context.xml 94B
example5.2.html 2KB
MANIFEST.MF 25B
WEB-INF
web.xml 972B
classes
.netbeans_automatic_build 0B
.netbeans_update_resources 0B
servlet
AjaxZipCodesServlet.class 3KB
AjaxUsernameServlet.class 2KB
DatabaseConnector.class 1KB
JSONUtil.class 2KB
lib
mysql-connector-java-5.1.7-bin.jar 693KB
ajax.js 2KB
index.html 2KB
generated-sources
ap-source-output
共 46 条
- 1
资源评论
weixin_42653672
- 粉丝: 93
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功