资源简介

最简单粗暴的mysql数据库访问,实现简单用户名和密码登录功能,最直接的演示 操作jsp+servlet+mysql

资源截图

代码片段和文件信息

package com.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Connection;

/**
 * Servlet implementation class LoginServlet
 */
//如果在web.xml中声明过servlet 就把这一句注释掉
//@WebServlet(“/LoginServlet“)
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
 * @see HttpServlet#doGet(HttpServletRequest request HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request HttpServletResponse response) throws ServletException IOException {
// TODO Auto-generated method stub
response.getWriter().append(“Served at: “).append(request.getContextPath());
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request HttpServletResponse response) throws ServletException IOException {
request.setCharacterEncoding(“utf-8“);
response.setCharacterEncoding(“utf-8“);
response.setContentType(“text/html; charset=UTF-8“);

//mysql数据库驱动
String driver = “com.mysql.jdbc.Driver“;
//链接数据库的url test为数据库名
String url = “jdbc:mysql://127.0.0.1:3306/test“;
//数据库用户
String user = “root“;
//数据库密码
String password = “yanglong123“;

//从前台读取到的用户名
String username = request.getParameter(“username“);
//从前台读取到的密码
String pwd = request.getParameter(“pwd“);

//数据库链接成功时返回的实例
Connection conn = null;
//查询成功时返回的记录集
ResultSet rs = null;
try{
//加载驱动
Class.forName(driver);
//获取链接
conn = (Connection) DriverManager.getConnection(url user password);
//准备sql查询
String sql = “select * from User where username=? and pwd=?“;
//使用PreparedStatement,可以防止sql注入
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1 username);
ps.setString(2 pwd);
//执行查询返回记录集 
rs = ps.executeQuery();
//如果查询到用户名和密码,则允许用户登录
if (rs.next()){
System.out.println(“login ok!!“);
PrintWriter out = response.getWriter();
out.flush();
out.println(“ript>“);
out.println(“alert(‘恭喜,登录成功‘);“);
out.println(“history.back();“);
out.println(“
ript>“);
out.close();
}else{
System.out.println(“login fail!!“);
PrintWriter out = response.getWriter();
out.flush();
out.println(“ript>“);
out.println(“alert(‘很遗憾,用户名或密码错误‘);“);
out.println(“history.back();“);
out.println(“
ript>“);
out.close();
}
//关闭PreparedStatement
ps.close();

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        838  2017-06-25 11:45  javaweb\mysql数据库\test.sql

     文件        483  2019-05-22 09:46  javaweb\readme.txt

     文件        937  2017-06-25 10:13  javaweb\sourcecode\mysqlTest\.classpath

     文件       1042  2017-06-25 10:12  javaweb\sourcecode\mysqlTest\.project

     文件        567  2017-06-25 10:12  javaweb\sourcecode\mysqlTest\.settings\.jsdtscope

     文件        364  2017-06-25 10:12  javaweb\sourcecode\mysqlTest\.settings\org.eclipse.jdt.core.prefs

     文件        479  2017-06-25 10:12  javaweb\sourcecode\mysqlTest\.settings\org.eclipse.wst.common.component

     文件        345  2017-06-25 10:12  javaweb\sourcecode\mysqlTest\.settings\org.eclipse.wst.common.project.facet.core.xml

     文件         49  2017-06-25 10:12  javaweb\sourcecode\mysqlTest\.settings\org.eclipse.wst.jsdt.ui.superType.container

     文件          6  2017-06-25 10:12  javaweb\sourcecode\mysqlTest\.settings\org.eclipse.wst.jsdt.ui.superType.name

     文件       3877  2017-06-25 11:33  javaweb\sourcecode\mysqlTest\build\classes\com\test\LoginServlet.class

     文件       3647  2017-06-25 11:33  javaweb\sourcecode\mysqlTest\src\com\test\LoginServlet.java

     文件     141182  2017-06-25 10:19  javaweb\sourcecode\mysqlTest\WebContent\css\bootstrap.css

     文件       1676  2017-06-25 11:26  javaweb\sourcecode\mysqlTest\WebContent\index.jsp

     文件      70843  2017-06-25 10:19  javaweb\sourcecode\mysqlTest\WebContent\js\jquery.js

     文件         39  2017-06-25 10:12  javaweb\sourcecode\mysqlTest\WebContent\meta-INF\MANIFEST.MF

     文件     536609  2017-06-02 09:08  javaweb\sourcecode\mysqlTest\WebContent\WEB-INF\lib\mysql-connector-java-5.0.7-bin.jar

     文件       1012  2017-06-25 10:47  javaweb\sourcecode\mysqlTest\WebContent\WEB-INF\web.xml

     目录          0  2017-06-25 11:44  javaweb\sourcecode\mysqlTest\build\classes\com\test

     目录          0  2017-06-25 11:44  javaweb\sourcecode\mysqlTest\build\classes\com

     目录          0  2017-06-25 11:44  javaweb\sourcecode\mysqlTest\src\com\test

     目录          0  2017-06-25 11:44  javaweb\sourcecode\mysqlTest\WebContent\WEB-INF\lib

     目录          0  2017-06-25 11:44  javaweb\sourcecode\mysqlTest\build\classes

     目录          0  2017-06-25 11:44  javaweb\sourcecode\mysqlTest\src\com

     目录          0  2017-06-25 11:44  javaweb\sourcecode\mysqlTest\WebContent\css

     目录          0  2017-06-25 10:19  javaweb\sourcecode\mysqlTest\WebContent\img

     目录          0  2017-06-25 11:44  javaweb\sourcecode\mysqlTest\WebContent\js

     目录          0  2017-06-25 11:44  javaweb\sourcecode\mysqlTest\WebContent\meta-INF

     目录          0  2017-06-25 11:44  javaweb\sourcecode\mysqlTest\WebContent\WEB-INF

     目录          0  2017-06-25 11:44  javaweb\sourcecode\mysqlTest\.settings

............此处省略10个文件信息

评论

共有 条评论