@RequestMapping(value="/vip",method = RequestMethod.GET)
public ModelAndView login1(final HttpSession httpSession) {
String phone = (String) httpSession.getAttribute("phone");
String viewname = "";
viewname = "vip";
String sql = "select * from user where phone ='"+phone+"' ";
String sql1 = "select vip from user where phone ='"+phone+"'";
System.out.println(sql);
final User user = new User();
jt.query(sql, new RowCallbackHandler(){
public void processRow(ResultSet rs) throws SQLException {
if (rs.isFirst()) {
user.setName(rs.getString(1));
user.setPhone(rs.getString(2));
user.setPassword(rs.getString(3));
user.setSex(rs.getString(4));
user.setVip(rs.getString(5));
}
}
});
jt.execute(sql1);
System.out.println(sql1);
httpSession.setAttribute("phone", phone);
ModelAndView mv = new ModelAndView(viewname);
mv.addObject("user", user);
return mv;
}
Statement接口的execute用于返回多个结果集的情况,不推荐使用。一般用的是executeQuery、executeUpdate。
executeQuery返回一个ResultSet对象用于得到数据库结果;
executeUpdate返回int类型得到影响的行数。
String sql = "select * from userinfo where id<?";
List resultList=jt.execute(sql,new PreparedStatementCallback() {
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
ps.setInt(1, 20);
ResultSet rs=ps.executeQuery();
Userinfo userinfo = new Userinfo();
List list=new ArrayList();
while (rs.next()) {
userinfo.setId(rs.getInt("id"));
userinfo.setUsername(rs.getString("username"));
userinfo.setPassword(rs.getString("password"));
userinfo.setCrateDate(rs.getDate("createDate"));
list.add(userinfo);
}
return list;
}
});
System.out.println(resultList.size());
//TODO 保存在数据到session
@RequestMapping(value="/vip",method = RequestMethod.GET)
public ModelAndView login1(final HttpSession httpSession) {
String phone = (String) httpSession.getAttribute("phone");
String viewname = "";
viewname = "vip";
String sql = "select * from user where phone ='"+phone+"' ";
String sql1 = "select vip from user where phone ='"+phone+"'";
System.out.println(sql);
final User user = new User();
jt.query(sql, new RowCallbackHandler(){
public void processRow(ResultSet rs) throws SQLException {
if (rs.isFirst()) {
user.setName(rs.getString(1));
user.setPhone(rs.getString(2));
user.setPassword(rs.getString(3));
user.setSex(rs.getString(4));
user.setVip(rs.getString(5));
}
}
});
final Vip users = new Vip();
jt.query(sql, new RowCallbackHandler(){
public void processRow(ResultSet rs1) throws SQLException {
if (rs1.isFirst()) {
users.setVip(rs1.getString("vip"));
String V=rs1.getString("vip");
httpSession.setAttribute("vip",V);
httpSession.setAttribute("V", rs1.getString("vip"));
System.out.println(V);
}
}
});
System.out.println(sql1);
httpSession.setAttribute("name", user.getName());
httpSession.setAttribute("sex", user.getSex());
httpSession.setAttribute("phone", phone);
ModelAndView mv = new ModelAndView(viewname);
mv.addObject("user", user);
mv.addObject("users",users);
return mv;
}
我的会员状态:${ users.vip }
<br>
<%=(String)session.getAttribute("V") %>
<%String V =(String)session.getAttribute("V");
if(V =="否") { %>
<a>未开通会员</a>
<% }if(V =="是") { %>
<a>已开通会员</a>
<% } %>
<br></td>