1.要想在 jsp 中向Servlet 传递数据,要么使用超链接,要么使用表单,要么使用session来传递,用request传递不过来
<%--用session传递::Servlet页面只能接收java代码中的session对象--%> <% String name = (String) request.getAttribute("name"); session.setAttribute("name",name); %> <%--Servlet.java--%> //从jsp接收数据(用request来传递,Servlet接收不到!) // String name = request.getParameter("name"); // System.out.println(name); String name = (String) request.getSession().getAttribute("name"); System.out.println(name);
2.从 jsp 页面通过表单传递数据:
在Servlet中可以通过 request.getParameter("name"); 来获取
3.设置编码格式
//设置编译格式 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8");
4. 1对应第一个问号,2对应第二个问号......
String sql = "insert into renkou01(hubie,type,mianji,num,name,id,sex,nation,educate) values(?,?,?,?,?,?,?,?,?)"; preparedStatement = null; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, hubie); preparedStatement.setString(2, type); preparedStatement.setString(3, mianji); preparedStatement.setString(4, num); preparedStatement.setString(5, name); preparedStatement.setString(6, id); preparedStatement.setString(7, sex); preparedStatement.setString(8, nation); preparedStatement.setString(9, educate);
String sql = "select * from renkou01 where name=?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, name);
5.判断一个字符串是否为整数
private boolean isNumeric(String str) { for (int i = str.length();--i>=0;){ if (!Character.isDigit(str.charAt(i))){ return false; } } return true; }
6.在Servlet.java页面编写内容在浏览器中显示:
PrintWriter write = response.getWriter(); write.write("面积不是整数"); write.close();
7.模糊查询
String name=request.getParameter("name"); Connection connection = DBUtil.getConnection(); String sql = "select * from renkou01 where name like ?"; // String sql = "select * from renkou01 where name like '%"+name+"%'"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,"%"+name+"%"); ResultSet rs= preparedStatement.executeQuery();
8.
String name = (String) request.getSession().getAttribute("name"); Connection connection = DBUtil.getConnection(); String sql="update renkou01 set hubie=?,type=?,mianji=?,num=?,nation=?,educate=? where name=?"; PreparedStatement preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,hubie); preparedStatement.setString(2,type); preparedStatement.setString(3,mianji); preparedStatement.setString(4,num); preparedStatement.setString(7,name); // preparedStatement.setString(6,id); // preparedStatement.setString(5,sex); preparedStatement.setString(5,nation); preparedStatement.setString(6,educate); int rs=preparedStatement.executeUpdate(); if(rs>0){ writer.write("修改成功"); } else{ writer.write("修改失败"); }
标签:preparedStatement,name,setString,request,笔记,sql,String From: https://www.cnblogs.com/hmy22466/p/16804755.html