Hi,歡迎訪問前端老白
省份:請選擇省份北京上海廣東
城市:請選擇城市
function changeCity() { var provinceId = document.getElementById("province").value; var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var citySelect = document.getElementById("city"); citySelect.innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "getCity.jsp?provinceId=" + provinceId, true); xmlhttp.send(); }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page import="java.sql.*" %><% String provinceId = request.getParameter("provinceId"); // 建立數據庫連接 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_name", "username", "password"); ps = conn.prepareStatement("SELECT * FROM city WHERE province_id = ?"); ps.setString(1, provinceId); rs = ps.executeQuery(); StringBuilder sb = new StringBuilder(); while (rs.next()) { sb.append("" + rs.getString("city_name") + ""); } out.println(sb.toString()); } catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } %>
老白網絡 (http://52shenghuonet.cn/) 前端 后端 zblog主題.網站地圖xml