本文是实现创建web项目,并实现web项目连接MySQL数据库,实现对数据库的增删查改。
本文需要的工具:
1.Intellij IDEA
2.Tomcat 8.5.466
3.java运行环境 jdk 1.8
4.数据库MySQL 8.0.21
5.java数据连接驱动 jdbc 8.0.21.
1.创建一个Web项目,并加入tomcat,按照一下步骤创建并加入tomca服务器。
2.加入mysql-connector-java-8.0.21.jar,需要在web目录下的Web-INF下创建一个lib目录把jar包加入进去。还要将jar包加入到项目中,详细过程见下图。
以上完成之后就可以进行数据库的操作,不过进行之前我们需要先测试tomcat服务器是否可以运行,下面,我们玉兴index.jsp页面是否可以正确的打开,在我这里是可以正常打开的,那么我们进行下一步连接数据库(默认数据库已经创建数据库和表)首先要编写连接数据库的代码,我们需要在src目录下床架一个dao目录(持久层)用于存放数据库操作的代码,一个domain目录(用于存放实体类),一个Test目录(用于测试代码),在dao层下创建一个connect类首先来连接数据库。
1.创建connect.java
2.测试数据库驱动(jar文件)是否加载成功。
3.连接成功,我们再写个个查询数据的查询语句用来查询表中的全部数据,并将结果输出在.jsp文件中。想要在页面中查询数据,我们需要在页面中写一个java程序片,用来连接数据库和调用对象中的查询数据库全部数据的某个方法,并将查询到的结果输出到页面的表中显示出来(查询操作需要将返回的结果集封装成对象,一行数据为一个对象,所以需要创建一个Student.java,并生成该类的set,get方法,该类放在src/domain目录下)。
public List selectAll(){
String selectAll="select * from student";
try {
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
try {
List list=new ArrayList<>();
resultSet = statement.executeQuery(selectAll);
while (resultSet.next()){
Student student=new Student();
student.setId(resultSet.getInt("id"));
student.setName(resultSet.getString("name"));
student.setEmail(resultSet.getString("email"));
student.setAge(resultSet.getInt("age"));
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
<%@ page import="java.sql.*" %><%--
Created by IntelliJ IDEA.
User: Jerry
Date: 2021/4/15
Time: 10:38
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="connect.Dao" %>
<%@ page import="java.util.List" %>
<%@ page import="domain.Student" %>
查询学生
<%
Dao dao=new Dao();
Connection connection=dao.getConnection();
if (connection!=null){
//out.println("数据库连接成功");
List list= dao.selectAll();
out.print("");
out.print("人员信息表 ");
out.print(" 序号 "
+ "id "
+ "姓名 "
+ "邮箱 "
+ "年龄 "
+ "操作 ");
int count = 0;
for (Student student:list){
out.print("");
out.print("" + (++count) + " ");
out.print("" + student.getId()+ " ");
out.print("" + student.getName() + " ");
out.print("" + student.getEmail() + " ");
out.print("" + student.getAge()+ " ");
out.print("" + "删除" + ""
+ " / 修改" + ""+ " ");
out.print(" ");
}
out.print("
");
connection.close();
connection.close();
}else {
out.println("数据库连接失败");
}
%>
代码解释:
首先解释select All()方法,该方法是用来查询所有数据的所以会产生一个结果集resultSet,然后再创建一个list集合将结果集数据遍历出来,放在student对象中,将对象放在list集合中,最后将集合通过循环的方法输出,在jsp文件中的程序片中调用连接数据库的方法,和调用selectAll()方法,实现数据库的查询。效果如下图。
按姓名查询的操作与查询全部数据的方法一样只不过是查询方法多了一个参数用来接收从页面传递过来的姓名参数,将参数写入sql语句。在这里我只把代码放在这里不做解释。
public List selectName(String Name){
String selectstudent="select * from student where name=?";
try {
String sql = "SELECT * FROM student where name=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, Name);
System.out.println(Name);
resultSet=preparedStatement.executeQuery();
while (resultSet.next()){
Student student=new Student();
student.setId(resultSet.getInt("id"));
student.setName(resultSet.getString("name"));
student.setEmail(resultSet.getString("email"));
student.setAge(resultSet.getInt("age"));
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
resultSet.close();
preparedStatement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
<%@ page import="connect.Dao" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="domain.Student" %>
<%@ page import="java.util.List" %><%--
Created by IntelliJ IDEA.
User: Jerry
Date: 2021/4/16
Time: 17:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
查询结果
<%
request.setCharacterEncoding("utf-8");
String Name=request.getParameter("name");
Dao dao=new Dao();
Connection connection=dao.getConnection();
if (connection!=null){
out.println("数据库连接成功");
List list= dao.selectName(Name);
out.print("");
out.print("人员信息表 ");
out.print(" 序号 "
+ "id "
+ "姓名 "
+ "邮箱 "
+ "年龄 "
+ "操作 ");
int count = 0;
for (Student student:list){
out.print("");
out.print("" + (++count) + " ");
out.print("" + student.getId()+ " ");
out.print("" + student.getName() + " ");
out.print("" + student.getEmail() + " ");
out.print("" + student.getAge()+ " ");
out.print("" + "删除" + ""
+ " / 修改" + ""+ " ");
out.print(" ");
}
out.print("
");
}else {
out.println("数据库连接失败");
}
%>
执行结果
public int insert(int Id,String Name,String Email,int Age){
String sql="insert into student values(?,?,?,?) ";
int i = 0;
try {
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1,Id);
preparedStatement.setString(2,Name);
preparedStatement.setString(3,Email);
preparedStatement.setInt(4,Age);
//System.out.println(id);
i=preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
preparedStatement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return i;
}
<%@ page import="connect.Dao" %>
<%@ page import="java.sql.Connection" %><%--
Created by IntelliJ IDEA.
User: Jerry
Date: 2021/4/16
Time: 20:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
插入数据
<%@ page import="connect.Dao" %>
<%@ page import="java.sql.Connection" %><%--
Created by IntelliJ IDEA.
User: Jerry
Date: 2021/4/16
Time: 21:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
提示页
<%
request.setCharacterEncoding("utf-8");
String id=request.getParameter("id");
String Name=request.getParameter("name");
String Email=request.getParameter("email");
String age=request.getParameter("age");
int ID=Integer.parseInt(id);
int Age=Integer.parseInt(age);
Dao dao=new Dao();
Connection connection=dao.getConnection();
if (connection!=null){
int i=dao.insert(ID,Name,Email,Age);
out.print("成功添加"+i+"条数据");
out.print("添加成功"+"返回首页" + "");
}else {
out.print("数据库连接数据失败");
out.print("添加失败"+"返回首页" + "");
}
%>
代码解释:增加数据的方法需要一个int类型的返回值用来表示增加数据的记录,方法的参数为数据库列名,数据类型需要与数据库中列的类型保持一致,在这里,我们需要用 int ID=Integer.parseInt(id);转化一下数据类型。
效果
public int deleteStudent(int ID){
String sql="delete FROM student where id=?";
int i=0;
try {
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1,ID);
i=preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
preparedStatement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return i;
}
<%@ page import="connect.Dao" %>
<%@ page import="java.sql.Connection" %><%--
Created by IntelliJ IDEA.
User: Jerry
Date: 2021/4/16
Time: 21:18
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
删除数据
<%
String id=request.getParameter("id");
int ID=Integer.parseInt(id);
out.print(ID);
Dao dao=new Dao();
Connection connection=dao.getConnection();
if(connection!=null){
int i=dao.deleteStudent(ID);
out.print("成功删除"+i+"条数据");
out.print("删除成功"+"返回首页" + "");
}else {
out.print("数据库连接失败");
out.print("删除失败"+"返回首页" + "");
}
%>
代码解释:该方法需要一个int类型的返回值,用来记录删除数据的条数。和一个参数用来接收方法需要的参数来填充sql语句。
效果:(我们这里有两个1005.所以会删除两条数据,这里是为验证返回值。)
public int update(String name,String email,int age,int id){
String sql="update Student set name =?,email=?,age=? where id=?";
int i=0;
try {
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,name);
preparedStatement.setString(2,email);
preparedStatement.setInt(3,age);
preparedStatement.setInt(4,id);
i=preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
preparedStatement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return i;
}
<%@ page import="connect.Dao" %>
<%@ page import="java.sql.Connection" %><%--
Created by IntelliJ IDEA.
User: Jerry
Date: 2021/4/16
Time: 21:47
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
修改数据
<%
String id;
id=request.getParameter("id");
int ID=Integer.parseInt(id);
%>
<%@ page import="connect.Dao" %>
<%@ page import="java.sql.Connection" %><%--
Created by IntelliJ IDEA.
User: Jerry
Date: 2021/4/16
Time: 21:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
提示页
<%
String id=request.getParameter("id");
String Name=request.getParameter("name");
String Email=request.getParameter("email");
String age=request.getParameter("age");
int ID=Integer.parseInt(id);
int Age=Integer.parseInt(age);
out.print(ID);
Dao dao=new Dao();
Connection connection=dao.getConnection();
if (connection!=null){
int i=dao.update(Name,Email,Age,ID);
out.print("成功修改了"+i+"条数据");
out.print("修改成功"+"返回首页" + "");
}else{
out.print("数据库连接失败");
out.print("修改失败"+"返回首页" + "");
}
%>
代码解释:该方法的返回值int记录修改数据的条数,方法参数为需要修改的内容,数据类型需要与数据库相匹配,要注意数据类型的转换。
效果: