首页 > 代码库 > javaWEB小练习:在数据库中查找相同的username和password

javaWEB小练习:在数据库中查找相同的username和password

/*练习题:
* 在Mysql数据库中创建一个person数据表,添加三个字段,id,user,password,并录入几条记录
*
*练习题:定义一个login.html,里面定义了两个请求字段:user,password,发送请求到loginServlet。
*再创建一个,LoginServlet(需要继承自HttpServlet,并重写doPost方法)
*在其中获取请求的user,password
*
*利用jdbc从person表中查询有没有和页面输入的user,password对应的记录
*
*若有则相应Hello:XXX,若没有,相应sorry:XXX XXX 为user
* */

------------------------------------------------------------------------------------------------------------------

首先在mysql中建立一个表名我person的表,然后再将连接数据库的开源包导进去

技术分享

------------------------------------------------------------------------------

在lib下面的web.xml文件配置为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<!-- loginServlet的配置和映射 -->
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.lanqiao.javatest.LoginServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<!-- /loginServlet是login.html中表单的提交方法 -->
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>


</web-app>

-------------------------------------------------------------------------------------------

在WebContent下面建立的表单login.html,使类,配置文件,表单建立起来了连接

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>表单的提交方法</title>
</head>
<body>
<form action="loginServlet" method="post">
<p>
username:<input type="text" name="username"/>
password:<input type="password" name="password"/>
<br><br>
<input type="submit" value="http://www.mamicode.com/Submit"/>
</p>
</form>
</body>
</html>

-------------------------------------------------------------------------------------

LoginServlet1类:

public class LoginServlet1 extends HttpServlet{


//HttpServlet: 是一个 Servlet, 继承自 GenericServlet. 针对于 HTTP 协议所定制.


@Override
//重写doPost方法
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out=resp.getWriter();

String username1=req.getParameter("username");
String password1=req.getParameter("password");

String sql="select count(id) from person where "
+ "username=? and password=?";
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
//获取连接数据库的文件
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql:///test";
String username2="root";
String password2="lxn123";

connection=DriverManager.getConnection(url, username2, password2);
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, username1);
preparedStatement.setString(2, password1);
resultSet=preparedStatement.executeQuery();

if(resultSet.next()){
int count=resultSet.getInt(1);
if (count>0) {
out.println("Hello:"+username1);
}
else{
out.println("Sorry:"+username1);
}

}
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (resultSet!=null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (preparedStatement!=null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}

javaWEB小练习:在数据库中查找相同的username和password