从数据库中读取数据(AJAX版本)

现在是用ajax 做从数据库读取数据的例子

1个jsp
Reg。jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ 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>
<script>
var xmlHttp;//不能用数字,标识符的定义

function creatXML(){//建立异步请求的对象,为了jsp和servlet的交互,固定写法,名字固定

if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
//调用的主要的方法
function a(){
creatXML();
xmlHttp.open("GET","http://127.0.0.1:9999/test730/servlet/zf");
xmlHttp.onreadystatechange=call;
xmlHttp.send(null);
}
//
function call(){
if(xmlHttp.readyState==4){//固定写法 4 代表完成
if(xmlHttp.status==200){//固定写法 200 代表 网页正常
var b=xmlHttp.responseXML.getElementsByTagName("rr");//按照标签的名字对应的是servlet里面的out.println("<rr>"+b.getCity()+"</rr>");
var c=document.getElementById("city");// <select name="city" id="city">里面的id="city"
for(var i=0;i<b.length;i++){//把b里面的数组做循环

var d=document.createElement("option");//option是固定的html标签
d.innerText=b[i].firstChild.nodeValue;//把城市输出的结果显示出来
d.setAttribute("value",b[i].firstChild.nodeValue);//输入value值
c.appendChild(d);//把option里面的东西放入到select标签里面

}
}
}
}
//测试用的方法
function c(){
alert("gag");
}
</script>
<body onload="a()">
<form id="form1" name="form1" method="post" action="reg.do">
用户名:
<label>
<input name="username" type="text" id="username" />
</label>
<p>电话:
<label>
<input name="tel" type="text" id="tel" />
</label>
</p>
<p>城市:
<label>
<select name="city" id="city">

</select>
</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
</p>
</form>
<input type="button" value="gaoying" onclick="a()"/>

</body>
</html>


1个servlet

package sl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javabean.yonghu;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import db.DB;

public class zf extends HttpServlet {

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/xml");//设置返回的信息的类型
PrintWriter out = response.getWriter();//为了显示输出。

ArrayList a =new DB().yonghu();

/**
* 做循环输出
*/

out.println("<citys>");
for(int i=0;i<a.size();i++){
yonghu b=(yonghu)a.get(i);
out.println("<rr>"+b.getCity()+"</rr>");
}
out.println("</citys>");
out.flush();//把流中的东西刷出去
out.close();//关闭
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


}

}

DB包

package db;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;

import javabean.yonghu;


public class DB {
private Connection conn;//用来连接数据库的“数据库连接对象”

private PreparedStatement stmt;//数据库操作对象

private ResultSet rs;

public DB() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test728", "root", "1234");
} catch (Exception e) {
e.printStackTrace();
}
}

public boolean adduser(String username,String tel,String city) {
try {

stmt = conn
.prepareStatement("insert into test728.user(username,tel,city) values(?,?,?)");
//stmt.setInt(1, name);
stmt.setString(1, username);
stmt.setString(2, tel);
stmt.setString(3, city);

stmt.execute();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;

}

public ArrayList yonghu(){
ArrayList a=new ArrayList();
try {

stmt=conn.prepareStatement("select distinct city from test728.user");


rs=stmt.executeQuery();
while(rs.next()){

yonghu c=new yonghu();
//c.setId(Integer.parseInt(rs.getString("id")));
//c.setUsername(rs.getString("username"));
//c.setTel("tel");
c.setCity(rs.getString("city"));
a.add(c);

}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return a;
}
}

1 个javabean

package javabean;

public class yonghu {
private int id;
private String username;
private String tel;
private String city;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>zf</servlet-name>
<servlet-class>sl.zf</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>zf</servlet-name>
<url-pattern>/servlet/zf</url-pattern>
</servlet-mapping>
</web-app>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值