jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>ajax根据省份获取id</title>
</head>
<body>
<script type="text/javascript">
//发起ajax请求,传参给服务器,服务器返回数据
dosearch=function () {
//1.创建异步对象
let XMLHttp=new XMLHttpRequest();
//2.绑定事件
XMLHttp.onreadystatechange=function () {
//处理
if (XMLHttp.readyState==4&&XMLHttp.status==200){
document.getElementById("name").value=XMLHttp.responseText;
}
}
let provinceid=document.getElementById("provinceid").value;
let param="provinceid="+provinceid;
XMLHttp.open("get","sea?"+param,true);
XMLHttp.send();
}
</script>
<table border="2px" align="center">
<tr>
<td>省份编号</td>
<td><input type="text" id="provinceid"></td>
<td><input type="button" value="搜索" onclick="dosearch()"></td>
</tr>
<tr>
<td>省份名称</td>
<td colspan="2"><input type="text" id="name"></td>
</tr>
</table>
</body>
</html>
get 代码
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id=request.getParameter("provinceid");
if (id!=""){
proviceDao name=new proviceDao();
Integer procinceid= Integer.parseInt(id);
String provincename= name.ProvinceNamegetbyid(procinceid);
PrintWriter pw=response.getWriter();
pw.print(provincename);
}
}
Dao
package syf;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//使用jdbc访问数据库
public class proviceDao {
JDBC jdbc=new JDBC();
//根据id获取名称
public String ProvinceNamegetbyid(Integer id){
String name="";
ResultSet res=null;
String sql="select name from province where id=?";
PreparedStatement pre=jdbc.createStatement(sql);
try {
pre.setInt(1,id);
res=pre.executeQuery();
while (res.next()){
name = res.getString("name");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return name;
}
}
JDBC工具
package syf;
import java.sql.*;
public class JDBC {
static final String url="jdbc:mysql://localhost:3306/syfwfl?useSSL=false&serverTimezone=UTC";
static final String user="root";
static final String password="19980605";
static PreparedStatement pre=null;
static ResultSet res=null;
static Connection conn=null;
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn=getconnection();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getconnection(){
try {
conn= DriverManager.getConnection(url,user,password);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return conn;
}
//创建mysql操作对象
public PreparedStatement createStatement(String sql){
try {
pre=conn.prepareStatement(sql);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return pre;
}
public void search(){}
public void close(){
if (res!=null) {
try {
res.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (pre!=null) {
try {
pre.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
//关闭了不能每次都连
if (conn!=null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}