JDBC是一套面向对象的应用程序接口,它制定了统一的访问各类关系数据库的标准接口,为各个数据库厂商提供了标准接口的实现。通过使用JDBC技术,开发人员可以用纯Java语言和标准的SQL语句编写完整的数据库应用程序,并且真正的实现了软件的跨平台型。
我们将通过一个简单的登录功能去验证JDBC的实用性,首先描述一下我们的工作:
1. 在MySQL或其他DBS中创建一个数据库TestDB(可以命名了其他名字),其中创建表user,表至少3个字段(主键,用户名,密码),然后向数据库中插入若干条记录,这样便有了用户帐号。
2. 程序需要两个jsp页面和一个Servlet类,一个jsp页面可以是login.jsp(输入用户名与密码的界面),另一个页面是success.jsp,登录成功后的跳转页面。
3. 在Servlet类中得到request里的用户名和密码,使用JDBC相关操作到数据库中查询,处理查询结果。可在Servlet的init方法中创建数据库的连接;在doPost方法中处理查询,最后进行转发处理,可使用forword方法。
4. 登录界面login.jsp如下,将用户在JSP页面输入的用户名username和密码password,通过servlet到数据库中验证用户信息。通过验证,跳转到success.jsp提示登录成功,否则,在login.jsp页码提示错误。
数据库方面的操作这里就不详细展开了,我们直接看一下效果图:
login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录界面</title>
</head>
<body>
<form action="Connect2DB" name="form1" id="form1" method="post">
<p> 用户名:<input type="text" name="username" id="username" /> </p>
<p> 密 码:<input type="password" name="userpassword" id="userpassword" /> </p>
<p> <input type="submit" value="提交" /> </p>
</form>
${warning }
</body>
</html>
连接数据库并处理登录页面提交信息的Servlet:
@WebServlet("/Connect2DB")
public class Connect2DB extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
String url = "jdbc:mysql://localhost:3306/testdb"; //数据库连接字符串
String name = "root";
String password = "root";
public Connect2DB() {
super();
}
public void init (ServletConfig config) throws ServletException {
super.init();
}
public void destory() {
}
protected void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username"); //从request中获取客户提交的数据
String userpassword = request.getParameter("userpassword");
boolean flag = false; //标记值
try {
Class.forName("com.mysql.jdbc.Driver"); //加载数据库驱动程序
Connection connection = DriverManager.getConnection(url, name, password); //连接数据库
Statement stmt = connection.createStatement();
String sql = "SELECT * FROM testdb.user"; //查询的SQL语句
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
if (username.equals(rs.getString("username")) && userpassword.equals(rs.getString("userpassword"))) {
flag = true;
}
}
if (flag) { //根据标记值的不同做不同的动作
request.setAttribute("welcome", "欢迎您回来:" + username); //增加显示登录者的小功能
request.getRequestDispatcher("success.jsp").forward(request, response);
}
else {
request.setAttribute("warning", "您输入的帐号或密码有错误,请重新输入!");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
} catch (ClassNotFoundException e) {
// TODO: handle exception
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
</head>
<body>
<p> 登录成功!</p>
<p> ${welcome }</p> <%EL表达式 %>
</body>
</html>
JDBC操作数据库(图解):