---
title: "从数据库中读取数据（AJAX版本）"
author: "qmug"
date: "2008-07-30T21:33:00Z"
tags: ["Ajax", "Java", "MySQL", "SQL Server", "Servlet"]
slug: "ajax"
draft: false
lastmod: "2008-07-30T21:33:00Z"
id: 221703
url: "https://qmug.iteye.com/blog/221703"
---

现在是用ajax 做从数据库读取数据的例子 1个jsp Reg。jsp \[code="java"] &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; &lt;%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.\*" errorPage="" %&gt; 无标题文档

用户名：

电话：

城市：

\[/code] 1个servlet \[code="java"] 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.  
\* * 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(""); for(int i=0;i"+b.getCity()+""); } out.println(""); out.flush();//把流中的东西刷出去 out.close();//关闭 } /\** * The doPost method of the servlet.  
\* * 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 { } } \[/code] DB包 \[code="java"] 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; } } \[/code] 1 个javabean \[code="java"] 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; } } \[/code] Web.xml \[code="java"] action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml debug 3 detail 3 0 This is the description of my J2EE component This is the display name of my J2EE component zf sl.zf action \*.do zf /servlet/zf \[/code]