maven的一个应用

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

 1.com.cdl.entity包

package com.cdl.entity;

public class User {

	    private Long id;

	    private String name;

	    private String loginName;

	    private String pwd;

	    private Long rid;

		public Long getId() {
			return id;
		}

		public void setId(Long id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getLoginName() {
			return loginName;
		}

		public void setLoginName(String loginName) {
			this.loginName = loginName;
		}

		public String getPwd() {
			return pwd;
		}

		public void setPwd(String pwd) {
			this.pwd = pwd;
		}

		public Long getRid() {
			return rid;
		}

		public void setRid(Long rid) {
			this.rid = rid;
		}

		public User() {
			super();
			// TODO Auto-generated constructor stub
		}

		@Override
		public String toString() {
			return "User [id=" + id + ", name=" + name + ", loginName=" + loginName + ", pwd=" + pwd + ", rid=" + rid + "]";
		}
	    
	    


}

2.com.cdl.util包

DBAccess

package com.cdl.util;

import java.io.InputStream;
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 DBAccess {
	private static String driver;
	private static String url;
	private static String user;
	private static String password;

	static {// 静�?�块执行�?次,加载 驱动�?�?
		try {
			InputStream is = DBAccess.class
					.getResourceAsStream("config.properties");

			Properties properties = new Properties();
			properties.load(is);

			driver = properties.getProperty("driver");
			url = properties.getProperty("url");
			user = properties.getProperty("user");
			password = properties.getProperty("pwd");

			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获得数据连接对象
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		try {
			Connection conn = DriverManager.getConnection(url, user, password);
			return conn;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public static void close(ResultSet rs) {
		if (null != rs) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Statement stmt) {
		if (null != stmt) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Connection conn) {
		if (null != conn) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Connection conn, Statement stmt, ResultSet rs) {
		close(rs);
		close(stmt);
		close(conn);
	}

	public static boolean isOracle() {
		return "oracle.jdbc.driver.OracleDriver".equals(driver);
	}

	public static boolean isSQLServer() {
		return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
	}
	
	public static boolean isMysql() {
		return "com.mysql.cj.jdbc.Driver".equals(driver);
	}

	public static void main(String[] args) {
		Connection conn = DBAccess.getConnection();
		System.out.println(conn);
		DBAccess.close(conn);
		System.out.println("isOracle�?" + isOracle());
		System.out.println("isSQLServer�?" + isSQLServer());
		System.out.println("isMysql�?" + isMysql());
		System.out.println("数据库连�?(关闭)成功");
	}
}

 DBHelper 

package com.cdl.util;

	import java.io.InputStream;
	import java.sql.Connection;
	import java.sql.DriverManager;
	import java.sql.PreparedStatement;
	import java.sql.ResultSet;
	import java.sql.SQLException;
	import java.sql.Statement;
	import java.util.Properties;

	/**
	 * 提供了一组获得或关闭数据库对象的方法
	 * 
	 */
	public class DBHelper {
		private static String driver;
		private static String url;
		private static String user;
		private static String password;

		static {// 静态块执行一次,加载 驱动一次
			try {
				InputStream is = DBHelper.class
						.getResourceAsStream("config.properties");

				Properties properties = new Properties();
				properties.load(is);

				driver = properties.getProperty("driver");
				url = properties.getProperty("url");
				user = properties.getProperty("user");
				password = properties.getProperty("pwd");

				Class.forName(driver);
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}

		/**
		 * 获得数据连接对象
		 * 
		 * @return
		 */
		public static Connection getConnection() {
			try {
				Connection conn = DriverManager.getConnection(url, user, password);
				return conn;
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}

		public static void close(ResultSet rs) {
			if (null != rs) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
		}

		public static void close(Statement stmt) {
			if (null != stmt) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
		}

		public static void close(Connection conn) {
			if (null != conn) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
		}
		
		public static void myClose(Connection con,PreparedStatement ps,ResultSet rs) {
			try {
				if(rs!=null) {
					rs.close();
				}
				if(ps!=null) {
					ps.close();
				}
				if(con!=null&&!con.isClosed()) {
					con.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		public static void close(Connection conn, Statement stmt, ResultSet rs) {
			close(rs);
			close(stmt);
			close(conn);
		}

		public static boolean isOracle() {
			return "oracle.jdbc.driver.OracleDriver".equals(driver);
		}

		public static boolean isSQLServer() {
			return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
		}
		
		public static boolean isMysql() {
			return "com.mysql.jdbc.Driver".equals(driver);
		}

		public static void main(String[] args) {
			Connection conn = DBHelper.getConnection();
			DBHelper.close(conn);
			System.out.println("isOracle:" + isOracle());
			System.out.println("isSQLServer:" + isSQLServer());
			System.out.println("isMysql:" + isMysql());
			System.out.println("数据库连接(关闭)成功");
		}
	}



 config.properties

#oracle9i
#driver=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@localhost:1521:ora9
#user=test
#pwd=test


#sql2005
#driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
#url=jdbc:sqlserver://localhost:1423;DatabaseName=test
#user=sa
#pwd=sa


#sql2000
#driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
#url=jdbc:microsoft:sqlserver://localhost:1433;databaseName=unit6DB
#user=sa
#pwd=888888

#mysql5
#driver=com.mysql.jdbc.Driver
#url=jdbc:mysql://127.0.0.1:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
#user=mybatis_ssm
#pwd=xiaoli

#mysql8
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=true
user=root
pwd=123456



3.com.cdl.dao

package com.cdl.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.cdl.entity.User;
import com.cdl.util.DBAccess;
import com.cdl.util.DBHelper;

public class UserDao {
//	登录
	public User login(User u){
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con=DBAccess.getConnection();
			String sql="select * from t_oa_user where loginName=? and pwd=?";
			ps = con.prepareStatement(sql);
			ps.setString(1, u.getLoginName());
			ps.setString(2, u.getPwd());
			rs=ps.executeQuery();
			if(rs.next()) {
				u.setId(rs.getLong(1));
				u.setName(rs.getString(2));
				u.setLoginName(rs.getString(3));
				u.setPwd(rs.getString(4));
				u.setRid(rs.getLong(5));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.close(con, ps, rs);
		}
		return u;
	}
}

4.com.cdl.web

package com.cdl.web;

import java.io.IOException;
import java.io.PrintWriter;

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 com.cdl.dao.UserDao;
import com.cdl.entity.User;

@WebServlet("/login.do")
public class UserAction extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置编码方式
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=UTF-8");
				
		//拿out
		PrintWriter out = resp.getWriter();
				
		UserDao ud=new UserDao();
		//接收表单传值
		String sname = req.getParameter("name");
		String pwd = req.getParameter("pwd");
		User u=new User();
		u.setLoginName(sname);
		u.setPwd(pwd);
		User u1 = ud.login(u);
		if(u1!=null) {
			out.print("<script>alert('登录成功');</script>");
		}else{
			out.print("<script>alert('登录失败');</script>");
		}
	}
}

页面login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="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>
	<h2>登陆 界面</h2>
	<form action="login.do">
		用户名:<input type="text" name="name"/>
		密码:<input type="text" name="pwd"/>
		<input type="submit" />
		<input type="reset" />
	</form>
</body>
</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更换

#oracle9i
#driver=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@localhost:1521:ora9
#user=test
#pwd=test


#sql2005
#driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
#url=jdbc:sqlserver://localhost:1423;DatabaseName=test
#user=sa
#pwd=sa


#sql2000
#driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
#url=jdbc:microsoft:sqlserver://localhost:1433;databaseName=unit6DB
#user=sa
#pwd=888888

#mysql5
#driver=com.mysql.jdbc.Driver
#url=jdbc:mysql://127.0.0.1:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
#user=mybatis_ssm
#pwd=xiaoli

#mysql8
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=true
user=root
pwd=123456



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

再运行

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值