核心提示:编程开发练习:是用Servlet实现用户登录界面,今天学习的是用Servlet实现用户登录界面。??index.jsp页面的代码还是07-08的代码??main.jsp页面的代码是新建页面的代码??今...
编程开发练习:是用Servlet实现用户登录界面,今天学习的是用Servlet实现用户登录界面。
??index.jsp页面的代码还是07-08的代码
??main.jsp页面的代码是新建页面的代码
??今天主要学习连接数据库
??下面是今天的代码
//BaseDao.java
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
// 负责完成数据库的操作
public class BaseDao {
// 增 删 查 改
// 为了能够操作数据库,我们需要首先获得一个操作数据库的代理
/**
* 所有数据库操作可以分为四步,
* 第一步 加载驱动
* 第二步 获取连接
* 第三步 执行操作
* 第四步 得到结果
*
* */
public int getData(String sql){
int result=0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sqsj","root","");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
result = 1;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
在这里,连接数据库的主要代码是:
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sqsj","root","");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
// LogServlet.java 这是一个Servlet类
package action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BaseDao;
public class LogServlet extends HttpServlet {
BaseDao bd = new BaseDao();
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter(); // 获取返回输出
//获取页面输入信息
String uname=request.getParameter("uname");
String upwd=request.getParameter("upwd");
String sql="select * from users where uname ='"+uname+"'and upwd='"+upwd+"'";
// System.out.println(request.getLocalAddr()); 获取IP地址 查javaEE帮助文档
if(bd.getData(sql)>0)
response.sendRedirect("../main.jsp"); // 重定向
else{
out.print("<script>alert('用户名或密码错误');history.back();</script>");
}
out.flush();
out.close();
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response); // Alt+/ 智能提示
}
}
在MyEclipse的文件如下:

此外,连接数据库还需要导入数据库的jar包,我在这里用的是MySql的数据库,我的数据库的界面如下

其中的测试数据如下:

以上,就是今天所学习的内容。


