AJAX局部刷新的选取省份例子

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();
                 }
             }
        }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

syf_wfl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值