Eclipse连接Mysql数据库实现登陆注册功能及遇到的一些问题
Eclipse连接Mysql数据库写不同角色的登陆注册功能及遇到的一些问题
建立概念模型并生成sql文件并添加数据
我使用的powerdesigner建立概念模型,然后转化成物理模型,由于我的sql server过期,我改用的navicat添加表的一些数据
生成mysql文件,注意是mysql(这里需要你在转化成物理模型时,就勾选mysql5.0)
在navicat中运行生成的sql文件,可能导入sql文件不成功,很可能是编码的问题,**可以把生成的sql文件用记事本打开另存,另存时可以选择编码为utf-8,**和你在navicat中创建的数据库字符集保持一致,都为utf-8。
重装mysql并利用eclipse连接数据库
为什么写重装呢?主要是因为我写代码连接不上,总是报这个错误:
javax.servlet. ServletException: java .sql.SQL Exception: Access denied for user’ root @‘localhost’ (using password: YES)
翻译过来就是:
拒绝用户“root”@“localhost”的访问(使用密码:YES)
在我换了3个版本的eclipse、修改驱动器版本以后,还是连不上(并且我不小心把我的mysql数据用户给删了,又不知道怎么恢复),我一激动就把它卸载了,在这里劝大家一句**,没事千万不要卸载mysql,因为如果没有卸载干净,重装真的太难了**,每次我以为我卸干净以后,运行安装包他都说“welcome back” ,而且安装包只能升级,不能安装(这肯定是没卸干净)
我大概用了7个小时解决这个问题,中途用了网上的各种方法,差点把百度背下来,求助了大佬,但是对于我而言这些方法都没有用,最后的解决办法是:**我重新下载了一个版本没那么新的安装包,这里我找不到链接了,但是真的有用,虽然安装以后没有图形化界面workbench,但还好我有navicat,**建立一个新的连接就行了。
Eclipse连接数据库
终于把mysql重装好了,回到代码上来,依然无法连接成功,还是原来的问题。
这个问题我真的找了好久,但还是连不上,最后同学让我把mysql的连接设置为不需要密码,神奇的事情发生了,真的能连上了。
关于如何设置不需要密码:
https://jingyan.baidu.com/article/d8072ac4853038ec94cefd58.html
这个页面只需要进行前面设置免密登陆就可以了,因为我的密码是正确的,所以没有修改root密码。
好不容易连上数据库了,但是因为我这次引用的我之前的项目,不知道为什么,我虽然修改了代码,但是运行访问的数据库还是我之前的数据库,clean了一下,还是不行,于是我新建了一个项目,把代码复制粘贴过去,然而还是没什么用,神奇的事情在这里发生了,创建新的项目运行以后,我回去之前的项目,发现就可以访问我现在指定的数据库了,这里没什么经验可以谈,感觉有运气的成分。
前端设计
首先是页面设计:
这里说几个点:
- 让图片模糊的css效果:
-webkit-filter: blur(2px);
-moz-filter: blur(2px);
-o-filter: blur(2px);
-ms-filter: blur(2px);
filter: blur(2px);
2. 让盒子透明的效果
3. 如何让三个盒子并排
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>商家主页</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
body
{
}
ul li p{
text-align: center;
font-size: 20px;
}
ul li span{
display: block;
font-size: 20px;
text-align: center;
color: #e08c35;
font-weight: bold;
}
.bg {
background: url(img/27.jpg) no-repeat fixed;
height:1050px;
text-align: center;
line-height:600px;
}
.bg-blur {
float: left;
width: 100%;
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
*{
margin: 0;
padding: 0;
list-style: none;
}
ul{
margin-top:200px;
height: 800px;
margin: 0 auto;
width: 800px;
filter :alpha(Opacity=60) ;
-moz -opacity:0.6;
opacity: 0.4;
}
ul li{
margin-top:200px;
background: #fff;
box-sizing: border-box;
border: 1px solid #333;
height: 400px;
width: 250px;
float: left;
margin-right: 20px;
}
ul li:last-child{
margin-right: 0px;
}
</style>
</head>
<body>
<div class="bg bg-blur">
<ul>
<li>
<p> 查看商品</p>
</li>
<li>
<p>可视化界面</p>
</li>
<li>
<p>数据详情</p>
</li>
</ul>
</div>
</body>
</html>
效果:
登陆功能
package Bean;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Servlet
*/
@WebServlet("/loginservlet")
public class loginservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String URL="jdbc:mysql://localhost:3306/cosme5?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
private static final String USERNAME="root";
private static final String PASSWORD="";
/**
* @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
//doPost(request,response);
Connection connection=null;
Statement statement=null;
String user = request.getParameter("name");
String password = request.getParameter("pwd");
String identity = request.getParameter("identity");//输入的身份
String[] isUserCookies = request.getParameterValues("isUserCookie");//记住密码
if(user==null||password==null)
{
request.getRequestDispatcher("loginfail.jsp").forward(request, response);
}
else
{
try
{
if ("business".equals(identity)) //身份是商家
{
Class.forName("com.mysql.cj.jdbc.Driver");
connection=DriverManager.getConnection(URL,USERNAME,PASSWORD);
statement=connection.createStatement();
final ResultSet dbresult=statement.executeQuery("select * from 商家 where 商家ID='"+user+"'and 商家密码='"+password+"'");
if(dbresult.next())
{
if (isUserCookies != null && isUserCookies.length > 0)//记住密码
{
saveCookie(user, password, response);
}
else
{
notSaveCookie(user, password, request, response);
}
request.getRequestDispatcher("BusinessIndex.jsp").forward(request, response);
}
else
{
request.getRequestDispatcher("loginfail.jsp").forward(request, response);
}
}
else//顾客
{
Class.forName("com.mysql.cj.jdbc.Driver");
connection=DriverManager.getConnection(URL,USERNAME,PASSWORD);
statement=connection.createStatement();
final ResultSet dbresult=statement.executeQuery("select * from 顾客 where 顾客ID='"+user+"'and 顾客密码='"+password+"'");
if(dbresult.next())
{
if (isUserCookies != null && isUserCookies.length > 0)//记住密码
{
saveCookie(user, password, response);
}
else
{
notSaveCookie(user, password, request, response);
}
request.getRequestDispatcher("CustomerIndex.jsp").forward(request, response);
}
else
{
request.getRequestDispatcher("loginfail.jsp").forward(request, response);
}
}
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(connection!=null)
connection.close();
if(statement!=null)
statement.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}
public void saveCookie(String inumber, String password, HttpServletResponse response)
{
Cookie inumberCookie = new Cookie("inumber", inumber);
Cookie passwordCookie = new Cookie("password", password);
inumberCookie.setPath("/");
passwordCookie.setPath("/");
inumberCookie.setMaxAge(864000); // 10 days
passwordCookie.setMaxAge(864000);
response.addCookie(inumberCookie);
response.addCookie(passwordCookie);
}
public void notSaveCookie(String inumber, String password, HttpServletRequest request, HttpServletResponse response)
{
Cookie[] cookies = request.getCookies();
for (Cookie cookie: cookies)
{
if (cookie.getName().equals("inumber") || cookie.getName().equals("password"))
{
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
注册功能
package Bean;
import java.io.IOException;
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 javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RegisterServlet
*/
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String URL="jdbc:mysql://localhost:3306/cosme5?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
private static final String USERNAME="root";
private static final String PASSWORD="";
/**
* @see HttpServlet#HttpServlet()
*/
public RegisterServlet() {
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());
Statement statement=null;
Connection connection=null;
PreparedStatement p=null;
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String identity=request.getParameter("identity");
String id=request.getParameter("ID");
String pwd=request.getParameter("Pwd");
// String password2=request.getParameter("password2");
String name=request.getParameter("Name");
String tel=request.getParameter("Tel");
String add=request.getParameter("Add");
if(id==""||pwd=="")
{
request.getRequestDispatcher("registerfail.jsp").forward(request, response);
}
else
{
try
{ //加载驱动
if ("business".equals(identity)) //身份是商家
{
Class.forName("com.mysql.cj.jdbc.Driver");
connection=DriverManager.getConnection(URL,USERNAME,PASSWORD);
statement=connection.createStatement();
final ResultSet dbresult=statement.executeQuery("select * from 商家 where 商家ID='"+id+"'");
if(dbresult.next())
{
request.getRequestDispatcher("registerfail.jsp").forward(request,response);
}
else
{
p=connection.prepareStatement("insert into 商家(商家ID,商家密码,商家昵称,商家电话,商家地址) values(?,?,?,?,?)");
p.setString(1,id);
p.setString(2,pwd);
p.setString(3,name);
p.setString(4,tel);
p.setString(5,add);
p.executeUpdate();
p.close();
request.getRequestDispatcher("registersuccessjsp.jsp").forward(request, response);
}
}
else//顾客注册
{
Class.forName("com.mysql.cj.jdbc.Driver");
connection=DriverManager.getConnection(URL,USERNAME,PASSWORD);
statement=connection.createStatement();
final ResultSet dbresult=statement.executeQuery("select * from 顾客 where 顾客ID='"+id+"'");
if(dbresult.next())
{
request.getRequestDispatcher("registerfail.jsp").forward(request,response);
}
else
{
p=connection.prepareStatement("insert into 顾客(顾客ID,顾客密码,顾客昵称,顾客电话,顾客地址) values(?,?,?,?,?)");
p.setString(1,id);
p.setString(2,pwd);
p.setString(3,name);
p.setString(4,tel);
p.setString(5,add);
p.executeUpdate();
p.close();
request.getRequestDispatcher("registersuccessjsp.jsp").forward(request, response);
}
}
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
/*finally
{
try
{
if(connection!=null)
connection.close();
if(statement!=null)
statement.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}*/
}
}
/**
* @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);
}
}
结束
这里没有贴上完整代码,但记录了我这几天遇到的问题和解决办法,算是一点收获吧。以后还要继续加油。
上一篇: php中变量的3中赋值方式
下一篇: js实现图片首尾平滑轮播