0. 链接
链接:https://pan.baidu.com/s/1x8of6Mhdg3RhLfE9WyEs4Q
提取码:ak1r
1. 实现效果
0. 数据库建立
1. 用户名或密码为空:require字段实现
2. 密码错误
3. 成功登录
2. 实现过程
0. 项目目录
1. 导入JAR包(官网下载)
2. 配置资源文件(用以链接数据库的基础数据)
3. 实现DAO工具类,解析资源文件,建立数据库链接
4. 查询数据库
5. 遍历数据
3. 代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.io.IOException; import java.sql.*; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import DaoUtils.JdbcUtils; /** * Servlet implementation class check */ @WebServlet("/check") public class check extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public check() { 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 Connection con = null; PreparedStatement preparedStatement = null; ResultSet rs = null; request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String userName = request.getParameter("checkInID"); String userPass = request.getParameter("checkInPass"); try { con = JdbcUtils.getConnection(); String sql = "SELECT * FROM USER WHERE userName = '" + userName + "' AND userPass = " + userPass; preparedStatement = con.prepareStatement("SELECT * FROM USER WHERE userName = ? AND userPass = ?"); preparedStatement.setString(1, userName); preparedStatement.setInt(2, Integer.valueOf(userPass)); rs = preparedStatement.executeQuery(); if(!rs.next()){ response.getWriter().write("用户名或密码错误"); }else{ response.getWriter().write("用户名:" + rs.getString(1)); response.getWriter().write("</br>"); response.getWriter().write("密码:" + rs.getInt(2)); } } catch (SQLException | ClassNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } finally { release(con, preparedStatement, rs); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } public static void release(Connection conn, Statement st, ResultSet rs){ if(rs != null){ try{ rs.close(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } rs = null; } if(st != null){ try{ st.close(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } st = null; } if(conn != null){ try{ conn.close(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } conn = null; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package DaoUtils; import java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class JdbcUtils { private static String driver = null; private static String url = null; private static String username = null; private static String password = null; public static void setDriver(String driver) { JdbcUtils.driver = driver; } public static void setPassword(String password) { JdbcUtils.password = password; } public static void setUsername(String username) { JdbcUtils.username = username; } public static void setUrl(String url) { JdbcUtils.url = url; } public static String getDriver() { return driver; } public static String getPassword() { return password; } public static String getUrl() { return url; } public static String getUsername() { return username; } static{ //try{ /*String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/checkprojectdb"; String username = "root"; String password = "980420";*/ Properties p = new Properties(); try{ //通过相对路径加载文件 String path = (Thread.currentThread().getContextClassLoader().getResource("").getFile()).substring(1); path=java.net.URLDecoder.decode(path, "utf-8"); path=path.replace('/', '\'); path=path.replace("file:", ""); path=path.replace("classes\", ""); path+="classes/DaoUtils/db.properties"; p.load(new FileInputStream(new File(path))); //用getProperties方法通过关键字获取信息 driver = p.getProperty("driver"); url = p.getProperty("url"); username = p.getProperty("username"); password = p.getProperty("password"); //} Class.forName(driver); }catch (Exception e) { // TODO: handle exception System.out.println(driver +" " + url + " " + username +" " + password ); throw new ExceptionInInitializerError(e); } } public static Connection getConnection() throws SQLException, ClassNotFoundException{ return DriverManager.getConnection(url, username, password); } public static void release(Connection conn, Statement st, ResultSet rs){ if(rs != null){ try{ rs.close(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } rs = null; } if(st != null){ try{ st.close(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } st = null; } if(conn != null){ try{ conn.close(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } conn = null; } } public static void main(String[] args) { System.out.println(driver + "; " + username + "; " + password + "; " + url); } }
4. 总结
遇到的问题:
1. 基础配置数据写错了
2. 数据库链接密码错误
在MySQL登录时出现Access denied for user 'root'@'localhost' (using password: YES) 拒绝访问