JSP数据库交互 5
<%request.setCharacterEncoding("GBK");Stringname=request.getParameter("user");Stringp...
<%
request.setCharacterEncoding("GBK");
String name =request.getParameter("user") ;
String pass = request.getParameter("pass");
User user = new User();
user.setName("name");
user.setPass("pass");
DaoImpl daoImpl = new DaoImpl();
user = daoImpl.findUser(name);
if(user!=null && pass!=null){
if(pass.equals(user.getName()) || name.equals(daoImpl.findUser(name)) ){
session.setAttribute("login",user.getName());
//response.sendRedirect("bb.jsp");
}else{
response.sendRedirect("index.jsp");
}
}
%>
<body>
<form action="bb.jsp" name = "myform" method="post">
<table>
<tr>
<td>用户名:<input type ="text" name="user"></td><td valign="top"><br></td>
</tr>
<tr>
<td>密 码:<input type ="text" name="pass" size ="20"></td><td valign="top"><br></td>
</tr>
<tr>
<td><input type ="submit" name="submit" value = "确定"></td><td valign="top"><br></td>
</tr>
</table>
</form>
//数据public class DaoImpl extends Dao{
public User findUser(String uName){
Connection conn=null;
PreparedStatement pstmt = null;
ResultSet rs = null;
User user1=null;
String sql = "select * from User_Table where name=?";
try{
conn = this.getConn();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, uName);
rs =pstmt.executeQuery();
if(rs.next()){
user1 = new User();
user1.setName(rs.getString(1));
user1.setPass(rs.getString(2));
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return user1;
}库代码数据库我用直连就不写了还有个实体类也不写了
数据库字段name ,pass我在一个Text上输入123 123 要实现和数据库交互帮我看看代码 谢谢!!! 展开
request.setCharacterEncoding("GBK");
String name =request.getParameter("user") ;
String pass = request.getParameter("pass");
User user = new User();
user.setName("name");
user.setPass("pass");
DaoImpl daoImpl = new DaoImpl();
user = daoImpl.findUser(name);
if(user!=null && pass!=null){
if(pass.equals(user.getName()) || name.equals(daoImpl.findUser(name)) ){
session.setAttribute("login",user.getName());
//response.sendRedirect("bb.jsp");
}else{
response.sendRedirect("index.jsp");
}
}
%>
<body>
<form action="bb.jsp" name = "myform" method="post">
<table>
<tr>
<td>用户名:<input type ="text" name="user"></td><td valign="top"><br></td>
</tr>
<tr>
<td>密 码:<input type ="text" name="pass" size ="20"></td><td valign="top"><br></td>
</tr>
<tr>
<td><input type ="submit" name="submit" value = "确定"></td><td valign="top"><br></td>
</tr>
</table>
</form>
//数据public class DaoImpl extends Dao{
public User findUser(String uName){
Connection conn=null;
PreparedStatement pstmt = null;
ResultSet rs = null;
User user1=null;
String sql = "select * from User_Table where name=?";
try{
conn = this.getConn();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, uName);
rs =pstmt.executeQuery();
if(rs.next()){
user1 = new User();
user1.setName(rs.getString(1));
user1.setPass(rs.getString(2));
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return user1;
}库代码数据库我用直连就不写了还有个实体类也不写了
数据库字段name ,pass我在一个Text上输入123 123 要实现和数据库交互帮我看看代码 谢谢!!! 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询