一、eclipse安装好,jdk安装好,mySQL安装好。
二、去官网下载JDBC https://dev.mysql.com/downloads/connector/j/
select Operating System:选择Platform independent。点击Download。
然后点No thanks, just start my download,不用登陆。

下载好后是个压缩包,解压后里面有一个jar文件,类似于这个名字mysql-connector-java-8.0.19.jar,就在第一层打开就看得到。
三、打开eclipse,创建一个Dynamic Web Project(右键->新建->Dynamic Web Project),找不到就在右键->新建->other里面找到web->Dynamic Web Project。(前提是tomcat什么的下载好奥,基础工作这里不讲了)
我新建的project是dbtest。然后打开WebContent文件夹->WEB-INF->lib,把刚刚的mysql-connector-java-8.0.19.jar移进去。

四、在src下面建立包
右键src->new->package(找不到在src->new->other->java->package)
(一般以com开头.企业名字) 
点finish后会变成这样

在utils包下面新建一个class(就是java的类)叫做JDBCUtils(这里的数据库名,和密码都是你自己的。怎么建数据库在第六点)
package cn.zjnu.mis.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
private static String Driver = "com.mysql.jdbc.Driver";
private static String url ="jdbc:mysql://localhost:3306/你自己的数据库名?characterEncoding=utf8";
private static String user ="root";
private static String password="你自己的密码";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(Driver);
conn = DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn,Statement stat,ResultSet rs) {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stat != null) {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
五、在WebContent下面新建jsp。(运行的时候要有对应数据库的,怎么新建在这堆代码下面)

delete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>删除操作</title>
</head>
<body>
<%
Connection conn = JDBCUtils.getConnection();
Statement stat = conn.createStatement();
String id = request.getParameter("id");
String sql = "delete from users where id ='"+id+"'";
//System.out.println(sql);
stat.execute(sql);
response.sendRedirect("showInfo.jsp");
%>
</body>
</html>
header.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*,cn.zjnu.mis.utils.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Login</title>
</head>
<style type="text/css">
div{
width:50%;
margin: 10px auto;
}
.w{
width:150px;
height:16px;
}
#box{
border:1px solid blue;
padding:20px;
}
</style>
<body>
<br>
<br>
<div id = "box">
<form action="validate.jsp" method = "post">
<div>
<label>Username:</label>
<input class="w" type="text" name ="username" >
</div>
<div>
<label>Password:</label>
<input class="w" type="password" name = "password" >
</div>
<div>
<input type="submit" value ="Login">
<input type="reset" value ="Reset">
<a href ="register.jsp">Regist</a>
</div>
<div style="color:red;"><%String msg = (String)request.getAttribute("msg");
if(msg !=null)out.print(msg);
%></div>
</form>
</div>
</body>
</html>
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center> <h1>用户注册</h1></center>
<form action="registerAction.jsp" method = "post">
<label>用户名:</label>
<input type="text" name="username"><br>
<label>密 码:</label><input type="password" name="password"><br>
<label>年 龄:</label><input type="text" name ="age"><br>
<input type="submit" value="regist"><input type="reset" value="reset">
</form>
</body>
</html>
registerAction.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf8");
Connection conn = JDBCUtils.getConnection();
Statement stat = conn.createStatement();
String name = request.getParameter("username");//request
String pass = request.getParameter("password");
String age = request.getParameter("age");
String sql = "insert into users(name,password,age) values('"+name+"','"+pass+"',"+age+")";
//System.out.println(sql);
stat.execute(sql);
session.setAttribute("users",name);
request.getRequestDispatcher("showInfo.jsp").forward(request,response);
%>
</body>
</html>
showInfo.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*,cn.zjnu.mis.utils.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>管理页面</title>
<style type="text/css">
table{
border: 2px solid blue;
padding:6px;
margin:6px;
}
</style>
</head>
<body>
<%
if(session.getAttribute("users")==null)response.sendRedirect("login.jsp");
Connection conn = JDBCUtils.getConnection();
Statement stat = conn.createStatement();
String sql = "select * from users";
ResultSet rs = stat.executeQuery(sql);
%>
<center><h1>用户管理</h1></center>
<center>
<table>
<tr><td>ID</td><td>用户名</td><td>密码</td><td>年龄</td><td>删除</td><td>修改</td></tr>
<%while(rs.next()){ %>
<tr><td><%=rs.getInt("id") %></td><td><%=new String(rs.getString("name")) %></td><td><%=rs.getString("password") %></td><td><%=rs.getInt("age") %></td><td><a href='delete.jsp?id=<%=rs.getInt("id") %>'>删除</a></td><td><a href='update.jsp?id=<%=rs.getInt("id") %>'>修改</a></td></tr>
<%} %>
</table>
</center>
</body>
</html>
updata.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file = "header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>更新操作</title>
</head>
<body>
<%
Connection conn = JDBCUtils.getConnection();
Statement stat = conn.createStatement();
String id = request.getParameter("id");
String sql = "select * from users where id='"+id+"'";
//String sql = "select * from users where id='4'";
ResultSet rs = stat.executeQuery(sql);
String username="",password ="";
int age = 18;
int nid = -1;
if(rs.next()){
nid =rs.getInt("id");
username= new String(rs.getString("name").getBytes("ISO-8859-1"));
password= rs.getString("password");
age = rs.getInt("age");
}
%>
<form action="updateAction.jsp" >
<label>用户名:</label>
<input type="text" name="username" value="<%=username %>" ><br>
<label>密 码:</label><input type="password" name="password" value="<%=password%>"><br>
<label>年 龄:</label><input type="text" name ="age" value="<%=age %>" ><br>
<input type="hidden" name="id" value="<%=nid%>"><br>
<input type="submit" value="update"> <input type="reset" value="reset">
</form>
</body>
</html>
updataAction.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file ="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String age = request.getParameter("age");
String id = request.getParameter("id");
Connection conn = JDBCUtils.getConnection();
Statement stat = conn.createStatement();
String sql = "update users set name ='"+username+"',password='"+password+"',age='"+age+"' where id = "+id;
//System.out.println(sql);
stat.executeUpdate(sql);
request.getRequestDispatcher("showInfo.jsp").forward(request, response);
%>
</body>
</html>
validate.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="cn.zjnu.mis.utils.*,java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>验证页面</title>
</head>
<body>
<%
//验证通过,进入到管理页面
//验证不通过,再次显示登录页面
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection conn = JDBCUtils.getConnection();
Statement stat = conn.createStatement();
String sql = "select * from users where name='"+username+"' and password='"+password+"'" ;
ResultSet rs = stat.executeQuery(sql);
if(rs.next()){
//验证通过,转向管理页面
JDBCUtils.close(conn, stat, rs);
session.setAttribute("users", username);
request.getRequestDispatcher("showInfo.jsp").forward(request,response);
}else{
//验证没有通过,重定向到登录页面
JDBCUtils.close(conn, stat, rs);
request.setAttribute("msg", "用户名或密码有错!!");
request.getRequestDispatcher("login.jsp").forward(request,response);
//response.sendRedirect("login.jsp");
}
%>
</body>
</html>
六、建立数据库
在terminal输入mysql -u root -p
然后输入密码,密码正确会像下图一样显示。

然后创建数据库(我命名数据库为mydb2,在JDBCUtils.java那边修改成mydb2)
在命令行输入create database mydb2;
创建成功后输入show databases;
显示mydb2在数据库内

进入mydb2,创建table
输入use mydb2进入该数据库
然后创建users表

输入desc users检查

然后数据库mydb2和表users就建立好了。
七、测试
打开eclipse,打开login.jsp,运行该jsp。

然后点Login啊,Regist啊,一个个试过去就好。
本文详细介绍了如何在Eclipse中配置MySQL数据库,并使用JSP进行数据操作,包括数据库连接、用户登录、注册、展示、更新和删除等功能实现。
1911

被折叠的 条评论
为什么被折叠?



