• maven的一个应用


    使用maven给WEB项目添加,完成 用户登陆+mysql数据库

     1.com.cdl.entity包

    1. package com.cdl.entity;
    2. public class User {
    3. private Long id;
    4. private String name;
    5. private String loginName;
    6. private String pwd;
    7. private Long rid;
    8. public Long getId() {
    9. return id;
    10. }
    11. public void setId(Long id) {
    12. this.id = id;
    13. }
    14. public String getName() {
    15. return name;
    16. }
    17. public void setName(String name) {
    18. this.name = name;
    19. }
    20. public String getLoginName() {
    21. return loginName;
    22. }
    23. public void setLoginName(String loginName) {
    24. this.loginName = loginName;
    25. }
    26. public String getPwd() {
    27. return pwd;
    28. }
    29. public void setPwd(String pwd) {
    30. this.pwd = pwd;
    31. }
    32. public Long getRid() {
    33. return rid;
    34. }
    35. public void setRid(Long rid) {
    36. this.rid = rid;
    37. }
    38. public User() {
    39. super();
    40. // TODO Auto-generated constructor stub
    41. }
    42. @Override
    43. public String toString() {
    44. return "User [id=" + id + ", name=" + name + ", loginName=" + loginName + ", pwd=" + pwd + ", rid=" + rid + "]";
    45. }
    46. }

    2.com.cdl.util包

    DBAccess

    1. package com.cdl.util;
    2. import java.io.InputStream;
    3. import java.sql.Connection;
    4. import java.sql.DriverManager;
    5. import java.sql.ResultSet;
    6. import java.sql.SQLException;
    7. import java.sql.Statement;
    8. import java.util.Properties;
    9. /**
    10. * 提供了一组获得或关闭数据库对象的方法
    11. *
    12. */
    13. public class DBAccess {
    14. private static String driver;
    15. private static String url;
    16. private static String user;
    17. private static String password;
    18. static {// 静�?�块执行�?次,加载 驱动�?�?
    19. try {
    20. InputStream is = DBAccess.class
    21. .getResourceAsStream("config.properties");
    22. Properties properties = new Properties();
    23. properties.load(is);
    24. driver = properties.getProperty("driver");
    25. url = properties.getProperty("url");
    26. user = properties.getProperty("user");
    27. password = properties.getProperty("pwd");
    28. Class.forName(driver);
    29. } catch (Exception e) {
    30. e.printStackTrace();
    31. throw new RuntimeException(e);
    32. }
    33. }
    34. /**
    35. * 获得数据连接对象
    36. *
    37. * @return
    38. */
    39. public static Connection getConnection() {
    40. try {
    41. Connection conn = DriverManager.getConnection(url, user, password);
    42. return conn;
    43. } catch (SQLException e) {
    44. e.printStackTrace();
    45. throw new RuntimeException(e);
    46. }
    47. }
    48. public static void close(ResultSet rs) {
    49. if (null != rs) {
    50. try {
    51. rs.close();
    52. } catch (SQLException e) {
    53. e.printStackTrace();
    54. throw new RuntimeException(e);
    55. }
    56. }
    57. }
    58. public static void close(Statement stmt) {
    59. if (null != stmt) {
    60. try {
    61. stmt.close();
    62. } catch (SQLException e) {
    63. e.printStackTrace();
    64. throw new RuntimeException(e);
    65. }
    66. }
    67. }
    68. public static void close(Connection conn) {
    69. if (null != conn) {
    70. try {
    71. conn.close();
    72. } catch (SQLException e) {
    73. e.printStackTrace();
    74. throw new RuntimeException(e);
    75. }
    76. }
    77. }
    78. public static void close(Connection conn, Statement stmt, ResultSet rs) {
    79. close(rs);
    80. close(stmt);
    81. close(conn);
    82. }
    83. public static boolean isOracle() {
    84. return "oracle.jdbc.driver.OracleDriver".equals(driver);
    85. }
    86. public static boolean isSQLServer() {
    87. return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
    88. }
    89. public static boolean isMysql() {
    90. return "com.mysql.cj.jdbc.Driver".equals(driver);
    91. }
    92. public static void main(String[] args) {
    93. Connection conn = DBAccess.getConnection();
    94. System.out.println(conn);
    95. DBAccess.close(conn);
    96. System.out.println("isOracle�?" + isOracle());
    97. System.out.println("isSQLServer�?" + isSQLServer());
    98. System.out.println("isMysql�?" + isMysql());
    99. System.out.println("数据库连�?(关闭)成功");
    100. }
    101. }

     DBHelper 

    1. package com.cdl.util;
    2. import java.io.InputStream;
    3. import java.sql.Connection;
    4. import java.sql.DriverManager;
    5. import java.sql.PreparedStatement;
    6. import java.sql.ResultSet;
    7. import java.sql.SQLException;
    8. import java.sql.Statement;
    9. import java.util.Properties;
    10. /**
    11. * 提供了一组获得或关闭数据库对象的方法
    12. *
    13. */
    14. public class DBHelper {
    15. private static String driver;
    16. private static String url;
    17. private static String user;
    18. private static String password;
    19. static {// 静态块执行一次,加载 驱动一次
    20. try {
    21. InputStream is = DBHelper.class
    22. .getResourceAsStream("config.properties");
    23. Properties properties = new Properties();
    24. properties.load(is);
    25. driver = properties.getProperty("driver");
    26. url = properties.getProperty("url");
    27. user = properties.getProperty("user");
    28. password = properties.getProperty("pwd");
    29. Class.forName(driver);
    30. } catch (Exception e) {
    31. e.printStackTrace();
    32. throw new RuntimeException(e);
    33. }
    34. }
    35. /**
    36. * 获得数据连接对象
    37. *
    38. * @return
    39. */
    40. public static Connection getConnection() {
    41. try {
    42. Connection conn = DriverManager.getConnection(url, user, password);
    43. return conn;
    44. } catch (SQLException e) {
    45. e.printStackTrace();
    46. throw new RuntimeException(e);
    47. }
    48. }
    49. public static void close(ResultSet rs) {
    50. if (null != rs) {
    51. try {
    52. rs.close();
    53. } catch (SQLException e) {
    54. e.printStackTrace();
    55. throw new RuntimeException(e);
    56. }
    57. }
    58. }
    59. public static void close(Statement stmt) {
    60. if (null != stmt) {
    61. try {
    62. stmt.close();
    63. } catch (SQLException e) {
    64. e.printStackTrace();
    65. throw new RuntimeException(e);
    66. }
    67. }
    68. }
    69. public static void close(Connection conn) {
    70. if (null != conn) {
    71. try {
    72. conn.close();
    73. } catch (SQLException e) {
    74. e.printStackTrace();
    75. throw new RuntimeException(e);
    76. }
    77. }
    78. }
    79. public static void myClose(Connection con,PreparedStatement ps,ResultSet rs) {
    80. try {
    81. if(rs!=null) {
    82. rs.close();
    83. }
    84. if(ps!=null) {
    85. ps.close();
    86. }
    87. if(con!=null&&!con.isClosed()) {
    88. con.close();
    89. }
    90. } catch (Exception e) {
    91. e.printStackTrace();
    92. }
    93. }
    94. public static void close(Connection conn, Statement stmt, ResultSet rs) {
    95. close(rs);
    96. close(stmt);
    97. close(conn);
    98. }
    99. public static boolean isOracle() {
    100. return "oracle.jdbc.driver.OracleDriver".equals(driver);
    101. }
    102. public static boolean isSQLServer() {
    103. return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
    104. }
    105. public static boolean isMysql() {
    106. return "com.mysql.jdbc.Driver".equals(driver);
    107. }
    108. public static void main(String[] args) {
    109. Connection conn = DBHelper.getConnection();
    110. DBHelper.close(conn);
    111. System.out.println("isOracle:" + isOracle());
    112. System.out.println("isSQLServer:" + isSQLServer());
    113. System.out.println("isMysql:" + isMysql());
    114. System.out.println("数据库连接(关闭)成功");
    115. }
    116. }

     config.properties

    1. #oracle9i
    2. #driver=oracle.jdbc.driver.OracleDriver
    3. #url=jdbc:oracle:thin:@localhost:1521:ora9
    4. #user=test
    5. #pwd=test
    6. #sql2005
    7. #driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
    8. #url=jdbc:sqlserver://localhost:1423;DatabaseName=test
    9. #user=sa
    10. #pwd=sa
    11. #sql2000
    12. #driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
    13. #url=jdbc:microsoft:sqlserver://localhost:1433;databaseName=unit6DB
    14. #user=sa
    15. #pwd=888888
    16. #mysql5
    17. #driver=com.mysql.jdbc.Driver
    18. #url=jdbc:mysql://127.0.0.1:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
    19. #user=mybatis_ssm
    20. #pwd=xiaoli
    21. #mysql8
    22. driver=com.mysql.cj.jdbc.Driver
    23. url=jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=true
    24. user=root
    25. pwd=123456

    3.com.cdl.dao

    1. package com.cdl.dao;
    2. import java.sql.Connection;
    3. import java.sql.PreparedStatement;
    4. import java.sql.ResultSet;
    5. import com.cdl.entity.User;
    6. import com.cdl.util.DBAccess;
    7. import com.cdl.util.DBHelper;
    8. public class UserDao {
    9. // 登录
    10. public User login(User u){
    11. Connection con=null;
    12. PreparedStatement ps=null;
    13. ResultSet rs=null;
    14. try {
    15. con=DBAccess.getConnection();
    16. String sql="select * from t_oa_user where loginName=? and pwd=?";
    17. ps = con.prepareStatement(sql);
    18. ps.setString(1, u.getLoginName());
    19. ps.setString(2, u.getPwd());
    20. rs=ps.executeQuery();
    21. if(rs.next()) {
    22. u.setId(rs.getLong(1));
    23. u.setName(rs.getString(2));
    24. u.setLoginName(rs.getString(3));
    25. u.setPwd(rs.getString(4));
    26. u.setRid(rs.getLong(5));
    27. }
    28. } catch (Exception e) {
    29. e.printStackTrace();
    30. }finally {
    31. DBHelper.close(con, ps, rs);
    32. }
    33. return u;
    34. }
    35. }

    4.com.cdl.web

    1. package com.cdl.web;
    2. import java.io.IOException;
    3. import java.io.PrintWriter;
    4. import javax.servlet.ServletException;
    5. import javax.servlet.annotation.WebServlet;
    6. import javax.servlet.http.HttpServlet;
    7. import javax.servlet.http.HttpServletRequest;
    8. import javax.servlet.http.HttpServletResponse;
    9. import com.cdl.dao.UserDao;
    10. import com.cdl.entity.User;
    11. @WebServlet("/login.do")
    12. public class UserAction extends HttpServlet{
    13. @Override
    14. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    15. doPost(req, resp);
    16. }
    17. @Override
    18. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    19. //设置编码方式
    20. req.setCharacterEncoding("utf-8");
    21. resp.setCharacterEncoding("utf-8");
    22. resp.setContentType("text/html; charset=UTF-8");
    23. //拿out
    24. PrintWriter out = resp.getWriter();
    25. UserDao ud=new UserDao();
    26. //接收表单传值
    27. String sname = req.getParameter("name");
    28. String pwd = req.getParameter("pwd");
    29. User u=new User();
    30. u.setLoginName(sname);
    31. u.setPwd(pwd);
    32. User u1 = ud.login(u);
    33. if(u1!=null) {
    34. out.print("");
    35. }else{
    36. out.print("");
    37. }
    38. }
    39. }

    页面login.jsp

    1. <%@ page language="java" contentType="text/html; charset=UTF-8"
    2. pageEncoding="UTF-8"%>
    3. html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    4. <html>
    5. <head>
    6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    7. <title>登陆title>
    8. head>
    9. <body>
    10. <h2>登陆 界面h2>
    11. <form action="login.do">
    12. 用户名:<input type="text" name="name"/>
    13. 密码:<input type="text" name="pwd"/>
    14. <input type="submit" />
    15. <input type="reset" />
    16. form>
    17. body>
    18. html>

    效果:

     输入正确用户名和密码

     

    原因:版本不同

    检查步骤

    第一步:检查项目中是否引入mysql-connector-java驱动包,如果没引入,请先引入对应版本的该包。

    第二步:检查mysql-connector-java驱动包的版本和配置的driver-class-name(驱动类名)是否一致。如果不一致会导致找不到Driver类,必须将版本和配置的驱动类名对应一致。

     结论:不同版本的mysql-connector-java驱动包的Driver类所在的位置不同,在5.x版本中Driver类在com.mysql.jdbc包路径下,到了6.x版中Driver类在com.mysql.cj.jdbc包路径下。

    如果我们在项目中配置的driver-class-name为com.mysql.jdbc.Driver,则对应的mysql-connector-java版本应该是5.x。

    如果我们在项目中配置的driver-class-name为com.mysql.cj.jdbc.Driver,则对应的mysql-connector-java版本应该是6.x。

     将driver-class-name更换

    1. #oracle9i
    2. #driver=oracle.jdbc.driver.OracleDriver
    3. #url=jdbc:oracle:thin:@localhost:1521:ora9
    4. #user=test
    5. #pwd=test
    6. #sql2005
    7. #driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
    8. #url=jdbc:sqlserver://localhost:1423;DatabaseName=test
    9. #user=sa
    10. #pwd=sa
    11. #sql2000
    12. #driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
    13. #url=jdbc:microsoft:sqlserver://localhost:1433;databaseName=unit6DB
    14. #user=sa
    15. #pwd=888888
    16. #mysql5
    17. #driver=com.mysql.jdbc.Driver
    18. #url=jdbc:mysql://127.0.0.1:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
    19. #user=mybatis_ssm
    20. #pwd=xiaoli
    21. #mysql8
    22. driver=com.mysql.jdbc.Driver
    23. url=jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=true
    24. user=root
    25. pwd=123456

     注意:我的依赖是5.x的 我就没更换依赖了

    再运行

     

  • 相关阅读:
    SAP MM会计凭证凭证状态为U
    2023年度中国测绘地理信息类期刊大全
    【算法|前缀和系列No.1】牛客网 DP34 【模板】前缀和
    桶排序(Bucket Sort)
    python psutil库之——获取网络信息(网络接口信息、网络配置信息、以太网接口、ip信息、ip地址信息)
    【华为OD机试真题 JS】根据某条件聚类最少交换次数
    【STM32】OLED
    【每日训练】连续最大和
    【论文笔记】Language Models are Few-Shot Learners
    【数组】最多能完成排序的块 数学
  • 原文地址:https://blog.csdn.net/weixin_62735525/article/details/126155066