新建项目
在webapp上右键添加新的目录起名为lib
导包,将jar包粘贴到lib目录中,右键jar包选择添加为库
连接tomcat并导入工件
两个工件都要导入
后连接数据库,输入账号密码后连接即可
在数据库中创建一个新的表
在webapp中右键新建jsp文件完成操作
index.jsp中写主页面,点击实现增删改查的进入
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>主页面</title>
</head>
<body>
<h2>主界面</h2>
<a href="zengshu.jsp" target="_self">
<button>增</button>
</a>
<a href="shanshu.jsp" target="_self">
<button>删</button>
</a>
<a href="gaishu1.jsp" target="_self">
<button>改</button>
</a>
<a href="chashu.jsp" target="_self">
<button>查</button>
</a>
</body>
</html>
zengshu.jsp中实现要增加的课程内容的输入
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="zeng.jsp">
<label for="k">课程:</label>
<input type="text" id="k" name="k"><br>
<label for="teacher">老师:</label>
<input type="text" id="teacher" name="teacher"><br>
<label for="address">教室:</label>
<input type="text" id="address" name="address"><br>
<input type="reset" value="重置">
<input type="submit" value="提交">
</form>
</body>
</html>
输入之后点击提交进入zeng.jsp,连接数据库并向库内插入已输入的内容,username为数据库账号,password为数据库密码,try catch中为连接数据库并插入的格式
<%@ page import="java.sql.*" %>
<%@ page import="java.nio.channels.Selector" %>
<%@ page import="java.util.Arrays" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%
String k=request.getParameter("k");
String teacher=request.getParameter("teacher");
String address=request.getParameter("address");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
String sql = "insert into class(k,teacher,address) values(?,?,?)";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1, k);
pst.setString(2, teacher);
pst.setString(3, address);
String n="添加完成";
request.setAttribute("n",n);
pst.executeUpdate();
connection.close();
pst.close();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
%>
<head>
<title>Title</title>
</head>
<body>
<font size=2>
${n}
</font><br>
<a href="index.jsp" target="_self">
<button>返回主界面</button>
</a>
</body>
</html>
shanshu.jsp为输入要删除的内容
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="shan.jsp">
<label for="k">课程:</label>
<input type="text" id="k" name="k"><br>
<input type="reset" value="重置">
<input type="submit" value="提交">
</form>
</body>
</html>
提交后进入shan.jsp,连接数据库和增加操作一样,根据
String sql = "";引号内的内容不同实现不同的功能,删除的格式为from后接表名,where后接要删除的内容
<%@ page import="java.sql.*" %>
<%@ page import="java.nio.channels.Selector" %>
<%@ page import="java.util.Properties" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%
String k=request.getParameter("k");
String n = null;
String driverClass="com.mysql.cj.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="zljzy962464";
Connection connection;
try{
Class.forName(driverClass);
connection=DriverManager.getConnection(url, user, password);
Statement statement=connection.createStatement();
String sql="delete from class where k = ?";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1,k);
int row = pst.executeUpdate();
if(row > 0){
n = "删除数据成功";
}else{
n = "您删除的数据不存在,请重新选择";
}
request.setAttribute("n",n);
pst.close();
connection.close();
}catch (Exception e){
e.printStackTrace();
}
%>
<head>
<title>删除</title>
</head>
<body>
<font size=2>
${n}
</font><br>
<a href="index.jsp" target="_self">
<button>返回主页面</button>
</a>
</body>
</html>
gaishu1.jsp中输入要修改的课程名称后提交给gaishu2.jsp输入要将该课程名称的其他内容修改为什么,进入后先判断数据库内是否有这个课程,如果有则继续输入,没有则提示没有
gaishu1.jsp:
<%@ page import="java.sql.*" %>gaishu2.jsp:
<%@ page import="java.nio.channels.Selector" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改界面</title>
</head>
<body>
<form action="gaishu2.jsp">
<label for="k">课程:</label>
<input type="text" id="k" name="k"><br>
<input type="reset" value="重置">
<input type="submit" value="提交">
</form>
</body>
</html>
<%@ page import="java.sql.*" %>gai.jsp中连接数据库并完成修改的操作,数据库语言为
<%@ page import="java.nio.channels.Selector" %>
<%@ page import="java.util.Properties" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.util.Objects" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改生产计划</title>
</head>
<body>
<%
String k = request.getParameter("k");
session.setAttribute("k",k);
String n = "修改的对象不存在";
int z = 0;
String driverClass="com.mysql.cj.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="zljzy962464";
Connection connection;
try{
Class.forName(driverClass);
connection=DriverManager.getConnection(url, user, password);
Statement statement=connection.createStatement();
String sql="select * from class";
ResultSet rs=statement.executeQuery(sql);
while(rs.next()){
if(Objects.equals(rs.getString("k"), k)) {
z++;
}
}
if(z==0)
{
request.setAttribute("n",n);
%>
<font size=2>
${n}
</font>
<a href="index.jsp" target="_self">
<button>返回主页面</button>
</a>
<%
}else{
%>
<form action="gai.jsp">
<label for="teacher">老师:</label>
<input type="text" id="teacher" name="teacher"><br>
<label for="address">教室:</label>
<input type="text" id="address" name="address"><br>
<input type="submit" value="提交">
</form>
<%
}
}catch (Exception e){
e.printStackTrace();
}
%>
</body>
</html>
update后接表名,set后接要修改的表中的数据的列名,where后为判断条件,判断是否为要修改的课程
<%@ page import="java.sql.*" %>
<%@ page import="java.nio.channels.Selector" %>
<%@ page import="java.util.Arrays" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%
String n = "修改成功";
request.setAttribute("n",n);
String k = session.getAttribute("k").toString();
String teacher=request.getParameter("teacher");
String address=request.getParameter("address");
String driverClass="com.mysql.cj.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="zljzy962464";
Connection connection;
try{
Class.forName(driverClass);
connection=DriverManager.getConnection(url, user, password);
Statement statement=connection.createStatement();
String sql="update class set teacher = ? ,address = ?where k = ?";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1,teacher);
pst.setString(2,address);
pst.setString(3,k);
pst.executeUpdate();
pst.close();
connection.close();
}catch (Exception e){
e.printStackTrace();
}
%>
<head>
<title>修改界面</title>
</head>
<body>
<font size=2>
${n}
</font><br>
<a href="index.jsp" target="_self">
<button>返回主页面</button>
</a>
</body>
</html>
chashu.jsp中输入要查询的老师,提交给cha.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="cha.jsp">
<label for="teacher">老师:</label>
<input type="text" id="teacher" name="teacher"><br>
<input type="reset" value="重置">
<input type="submit" value="提交">
</form>
</body>
</html>
进入gai.jsp后连接数据库,查找的数据库语言为:
String sql="select * from class";
class为要查找的表名,from后接要查找的表名
<%@ page import="java.sql.*" %>每个输入界面中的<label action=" ">中的href后的引号为要传入自己在该页面输入的内容到的那个页面,在那个页面中可以利用request.getParameter("")语句来接受上个页面传入的数据
<%@ page import="java.nio.channels.Selector" %>
<%@ page import="java.util.Arrays" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%
String n = "修改成功";
request.setAttribute("n",n);
String k = session.getAttribute("k").toString();
String teacher=request.getParameter("teacher");
String address=request.getParameter("address");
String driverClass="com.mysql.cj.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="zljzy962464";
Connection connection;
try{
Class.forName(driverClass);
connection=DriverManager.getConnection(url, user, password);
Statement statement=connection.createStatement();
String sql="update class set teacher = ? ,address = ?where k = ?";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1,teacher);
pst.setString(2,address);
pst.setString(3,k);
pst.executeUpdate();
pst.close();
connection.close();
}catch (Exception e){
e.printStackTrace();
}
%>
<head>
<title>修改界面</title>
</head>
<body>
<font size=2>
${n}
</font><br>
<a href="index.jsp" target="_self">
<button>返回主页面</button>
</a>
</body>
</html>
try {以上的为连接数据库的基本步骤,之后根据自己不同的需求,定义String sql = " ";根据引号内不同的语句可以完成不同的操作。
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "zljzy962464";
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
pst.executeUpdate();该语句为添加后对数据库内的内容更新,如果无该语句则添加的内容无法存入数据库中 标签:,String,request,connection,jsp,password,pst From: https://www.cnblogs.com/zljzy/p/17875318.html