JavaWeb 网页链接数据库增删改查

article/2025/11/10 18:50:15

  今天是web的第五节课,上一节课只涉及到了查询和增加,这一节课我们学习网页链接数据库进行增删改查,今天的内容更多的也是代码,理论内容较少,现在小编带大家进入新的一课。


 

目录

一.查

 二.增

三.删

四.修

五.数据库关系图 


一.查

   思路:

  • 写一个登录界面。
  • 我们建立一个新的表格,存放用户的数据。可自行往该表格中插入几条数据,方便测试
  • 判断数据是否存在,我们需要一个新的jsp文件,在该文件中,拿到登录界面的数据,  在连接数据库,查询该用户是否存在。存在的话跳转到首页,不存在的话直接跳转回到 登录界面面

  登录界面代码 

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><title>Document</title><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="/bootstrap-3.3.7-dist/css/bootstrap.css"><script src="/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script><script src="/bootstrap-3.3.7-dist/js/bootstrap.js"></script><style>* {outline: none !important;}html,body {background: #1abe9c;}form {width: 300px;background: #ebeff2;box-shadow: 0px 0px 50px rgba(0, 0, 0, .5);border-radius: 5px;padding: 20px;position: absolute;left: 50%;top: 50%;transform: translate(-50%, -50%);}.btn-group {width: 100%;}.btn-group button {width: 50%;}</style>
</head><body><form action="doLogin.jsp" method="post"><h3 class="text-center">欢迎使用🐖币新闻管理</h3><div class="form-group"><input name="username" type="text" class="form-control" placeholder="请输入您的用户名"></div><div class="form-group"><input name="userpwd" type="password" class="form-control" placeholder="请输入您的密码"></div><div class="btn-group"><button type="submit" class="btn btn-primary">登录</button><button type="button" class="btn btn-danger" onclick='location=href="regiest.html"'>没有账号?</button></div></form></body>
</html>

 判断用户是否存在数据库代码

   该代码拿到登录界面的数据,在去数据库进行匹配,匹配该用户是否存在

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%//拿到数据String name=request.getParameter("username");String pwd=request.getParameter("userpwd");//获取驱动Class.forName("oracle.jdbc.driver.OracleDriver");//编写连接语句String url="jdbc:oracle:thin:@localhost:1521:orcl";//获得连接对象Connection con=DriverManager.getConnection(url,"scott","zking123");//编写数据库语句PreparedStatement ps=con.prepareStatement("select * from jw05_user where uname=? and upwd=?");//给占位符赋值ps.setString(1,name);ps.setString(2, pwd);//获得结果集ResultSet rs=ps.executeQuery();//判断该用户是否存在if(rs.next()){//存在的话跳转到新闻首页//转发request.getRequestDispatcher("/news/index.jsp").forward(request, response);}else{//用户不存在数据库中,返回登录界面//重定向response.sendRedirect("login.jsp");}//关闭资源if(con!=null&&con.isClosed()){con.close();}if(ps!=null){ps.close();}if(rs!=null){ps.close();}
%>

 二.增

  思路:

  •  当登录成功用户会跳转到我们的首页,我们需要去发布新闻,点击新闻管理--->新闻发布。(点击这里跳转到增加新闻界面)
  • 发布新闻其实相当于增加数据,我们拿到发布界面上我们填写的数据,点击增加按钮加入进数据库中,新发布的新闻,也会在新闻首页显示出来(拿到数据需要像登录界面一样,创建新的一个jsp文件,拿到数据,连接数据库,把数据插入数据库)
  • 创建一张新闻表,存放新闻,上一节课04教大家是如何把数据增加进数据库,今天把数据加入数据库,在新闻首页界面新增的新闻会显示出来。
     

 新闻首页代码

   大家可以看到在新闻首页代码这里,我们也运用到了查,把新闻表中的数据拿出来,赋值给到页面上。

 ${pageContext.request.contextPath}/news/read.jsp?newId=<%=rs.getInt(1)%>

 这串代码是当我们点击新闻时,跳转到新闻阅读界面,newId是一个名字 ,rs.getInt(1)是新闻表的id,在后面加上newid=xxxx是携带一个数据过去,上方代码就是携带新闻你点击的这一篇新闻的id跳转到阅读界面中。该代码在我们的阅读界面起到非常大的作用,阅读界面可以拿到该id,查询该新闻数据,把数据赋值在阅读界面上。

  

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><title>bootstrap</title><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="/JavaWeb05/bootstrap-3.3.7-dist/css/bootstrap.css"><script src="/JavaWeb05/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script><script src="/JavaWeb05/bootstrap-3.3.7-dist/js/bootstrap.js"></script><style>* {outline: none !important;}body,html {background: #7f8d90;}nav,.breadcrumb {border-radius: 0px !important;margin-bottom: 0px !important;}.breadcrumb {margin-bottom: 20px !important;background: #36485c;color: white;}li h4 {width: 300px;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;}.breadcrumb .active {color: yellow;}</style>
</head><body>
<nav class="navbar navbar-default hidden-sm hidden-xs"><div class="container-fluid"><div class="navbar-header"><a class="navbar-brand" href="index.html" style="font-size: 25px;">🐖</a></div><ul class="nav navbar-nav"><li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown"> 新闻管理<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="/JavaWeb05/news/add.jsp">新闻发布</a></li><li class="divider"></li><li><a href="#">类别管理</a></li></ul></li></ul><ul class="nav navbar-nav navbar-right"><li><a>245@qq.com</a></li><li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li></ul></div>
</nav><ol class="breadcrumb"><li>您当前的位置是</li><li>新闻发布系统</li><li class="active">首页</li>
</ol><form class="form-inline" style="margin: 0px auto 20px;"><div class="form-group" style="display: block;text-align: center;"><div class="input-group"><div class="input-group-addon">新闻标题</div><input type="text" class="form-control" placeholder="请在此输入搜索的关键字"><span class="input-group-btn"><button type="submit" class="btn btn-primary">搜索🔍</button></span></div></div>
</form><%//创建一个新闻数据库,把数据库中的新闻数据,拿出来//获取驱动Class.forName("oracle.jdbc.driver.OracleDriver");//编写连接语句String url="jdbc:oracle:thin:@localhost:1521:orcl";//获得连接对象Connection con=DriverManager.getConnection(url,"scott","zking123");//编写数据库语句PreparedStatement ps=con.prepareStatement("select * from jw05_news");//获得结果集ResultSet rs=ps.executeQuery();while(rs.next()){
%><li class="list-group-item"><h4 class="list-group-item-heading"><a href="${pageContext.request.contextPath}/news/read.jsp?newId=<%=rs.getInt(1)%>" data-placement="bottom" data-toggle="tooltip" href="" title="国家卫健委:昨日新增确诊病例29例,其中本土病例2例"><%=rs.getString(2)%></a></h4><p class="list-group-item-text text-right"><span class="glyphicon glyphicon-user"><code><%=rs.getString(4)%></code></span><span class="glyphicon glyphicon-eye-open"><code>1000</code></span><span class="glyphicon glyphicon-tag"><code>1000</code></span><span class="glyphicon glyphicon-time"><code><%=rs.getString(5)%></code></span></p></li><%}if (!con.isClosed()) {con.close();}ps.close();rs.close();%></ul>
<div class="container text-center"><ul class="pagination" style="margin: 20px auto;"><li><a href="#"><span>&laquo;</span></a></li><li><a href="#">1</a></li><li><a href="#">2</a></li><li><a href="#">3</a></li><li><a href="#">4</a></li><li><a href="#">5</a></li><li><a href="#"><span>&raquo;</span></a></li></ul>
</div>
<script>$(function () {$('[data-toggle="tooltip"]').tooltip({trigger: "hover"})})
</script>
</body>
</html>

增加新闻界面代码

   点击新闻发布跳转到增加新闻的界面,在这里有一个下拉框选项,该下拉框选项的数据,由我们连接新闻表,拿到新闻类别的数据,赋值给下拉框。

   大家要给输入框和下拉框定义名字,方便拿到数据

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><title>bootstrap</title><meta content="width=device-width, initial-scale=1" name="viewport"><link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet"><script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script><script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script><style>* {outline: none !important;}body,html {background: #7f8d90;}nav,.breadcrumb {border-radius: 0 !important;margin-bottom: 0 !important;}.breadcrumb {margin-bottom: 20px !important;background: #36485c;color: white;}input,select,textarea,.panel-heading {border: none !important;border-radius: 0 !important;}.breadcrumb .active {color: yellow;}</style>
</head><body>
<nav class="navbar navbar-default hidden-sm hidden-xs"><div class="container-fluid"><div class="navbar-header"><a class="navbar-brand" href="${pageContext.request.contextPath}/news/index.jsp"style="font-size: 25px;">🐖</a></div><ul class="nav navbar-nav"><li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown">新闻管理<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="#">新闻发布</a></li><li class="divider"></li><li><a href="#">类别管理</a></li></ul></li></ul><ul class="nav navbar-nav navbar-right"><li><a>245@qq.com</a></li><li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li></ul></div>
</nav><ol class="breadcrumb"><li>您当前的位置是</li><li>新闻发布系统</li><li class="active">新闻发布</li>
</ol><form action="doAdd.jsp" class="container" method="post"><div class="panel panel-info"><div class="panel-heading">新闻标题</div><input class="form-control" name="title" maxlength="50" placeholder="标题控制在30个字之内哦~~~" required><div class="panel-heading">新闻类别</div><select class=" form-control" name="topic"><!-- 这一步是拿到数据库新闻类别的值,赋值到新闻类别 --><%//获取驱动Class.forName("oracle.jdbc.driver.OracleDriver");//编写连接语句String url="jdbc:oracle:thin:@localhost:1521:orcl";//获得连接对象Connection con=DriverManager.getConnection(url,"scott","zking123");//编写数据库语句PreparedStatement ps=con.prepareStatement("select * from jw05_topic");//获得结果集ResultSet rs=ps.executeQuery();while(rs.next()){%><option value=<%=rs.getInt(1) %>><%=rs.getString(2)%></option><%}%></select><div class="panel-heading">新闻作者</div><input class="form-control" name="author" maxlength="10" placeholder="名字控制在10个字之内哦~~~" required><div class="panel-heading">发布时间</div><input class="form-control" name="publisher" required type="date"><div class="panel-heading">新闻内容</div><textarea class="form-control" name="content" placeholder="🙅‍达咩~~~~这是必填的" required rows="10"></textarea><div class="panel-footer"><button class="btn btn-primary" >增加</button><button class="btn btn-danger">取消</button></div></div>
</form></body>
</html>

获取数据 插入数据库代码(增加)

该jsp文件中编写的代码,拿到增加界面的数据,通过输入框和下拉框的名字拿到数据,

连接数据库,将数据增加进数据库,在判断是否增加成功。

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%//拿到数据//拿到新闻标题String title=request.getParameter("title");String topic=request.getParameter("topic");String author=request.getParameter("author");String publisher=request.getParameter("publisher");String content=request.getParameter("content");//获取驱动Class.forName("oracle.jdbc.driver.OracleDriver");//编写连接语句String url="jdbc:oracle:thin:@localhost:1521:orcl";//获得连接对象Connection con=DriverManager.getConnection(url,"scott","zking123");//编写数据库语句PreparedStatement ps=con.prepareStatement("select  nvl(max(news_id),0) from jw05_news");ResultSet rs=ps.executeQuery();//拿到idint id=0;if(rs.next()){id=rs.getInt(1);}id++;ps=con.prepareStatement("insert into jw05_news vaules(news_id,news_title,news_topic, news_author, news_publisher, news_content) values(?,?,?,?,?,?)");ps.setInt(1, id);ps.setString(2, title);ps.setString(3, topic);ps.setString(4, author);ps.setString(5, publisher);ps.setString(6, content);int i=ps.executeUpdate();if(i>0){out.print("<script>alert('增加成功');location.href='/JavaWeb05/news/index.jsp'</script>");}else{ out.print("<script>alert('增加失败');location.href='/JavaWeb05/news/index.jsp'</script>");}//关闭资源if(con!=null&&con.isClosed()){con.close();}if(ps!=null){ps.close();}if(rs!=null){rs.close();}%>

 阅读新闻代码

 该代码是我们在新闻首页时,点击新闻的标题跳转到该界面,该界面可以进行删除新闻,修改新闻操作。

  我们跳转到该界面的时候,这个界面其实相当于,详细的看我们的新闻, 我们会在新闻首页跳转过来时,新闻首页有一串代码会将新闻的id传过来,在一串网址上面会携带一个xxx=xx,就是传过来的id,然后用request.getParameter拿到该id,在连接数据库,用id查询,赋值给到界面上面。

<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><title>bootstrap</title><meta content="width=device-width, initial-scale=1" name="viewport"><link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet"><script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script><script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script><style>* {outline: none !important;}body,html {background: #7f8d90;}nav,.breadcrumb {border-radius: 0 !important;margin-bottom: 0 !important;}.breadcrumb {margin-bottom: 20px !important;background: #36485c;color: white;}input,select,textarea,.panel-heading {border: none !important;border-radius: 0 !important;}.breadcrumb .active {color: yellow;}</style>
</head><body>
<nav class="navbar navbar-default hidden-sm hidden-xs"><div class="container-fluid"><div class="navbar-header"><a class="navbar-brand" href="${pageContext.request.contextPath}/news/index.jsp"style="font-size: 25px;">🐖</a></div><ul class="nav navbar-nav"><li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown">新闻管理<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="#">新闻发布</a></li><li class="divider"></li><li><a href="#">类别管理</a></li></ul></li></ul><ul class="nav navbar-nav navbar-right"><li><a>245@qq.com</a></li><li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li></ul></div>
</nav><ol class="breadcrumb"><li>您当前的位置是</li><li>新闻发布系统</li><li class="active">新闻阅读</li>
</ol>
<%
//拿到传过来的idString newId=request.getParameter("newId");
//1.获取驱动Class.forName("oracle.jdbc.driver.OracleDriver");//2.定义连接字符String url="jdbc:oracle:thin:@localhost:1521:orcl";//3.获得连接Connection con=DriverManager.getConnection(url,"scott","zking123");//4.获得执行对象PreparedStatement ps=con.prepareStatement("select *from jw05_news where news_id=? ");ps.setInt(1,Integer.parseInt(newId));ResultSet rs=ps.executeQuery();//定义需要的值String title="";int count=0;String author="";String publisher="";String content="";if(rs.next()){title=rs.getString(2);count=rs.getInt(3);author=rs.getString(4);publisher=rs.getString(5);content=rs.getString(6);}
%>
<div class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;"><h1><%=title%></h1><h3 class="text-right"><small><span class="glyphicon glyphicon-user"><span class="label label-default"></span><%=author%></span><span class="glyphicon glyphicon-eye-open"><span class="label label-default"></span>1000</span><span class="glyphicon glyphicon-time"><span class="label label-info"></span>1000</span></small></h3><samp></samp><div class="btn-group btn-group-justified" style="margin-bottom: 20px;"><div class="btn-group"><a href="${pageContext.request.contextPath}/news/doDel.jsp?newId=<%=newId%>" class="btn btn-danger" type="button">删除</a></div><div class="btn-group"><a href="${pageContext.request.contextPath}/news/upd.jsp?newId=<%=newId%>" class="btn btn-info" type="button">修改</a></div></div>
</div><div class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;margin-top: 10px;"><div class="panel panel-default" style="margin-top: 20px;"><div class="panel-heading"><span class="glyphicon glyphicon-user"><span class="label label-success">xxx</span></span><p style="margin-top: 10px;text-indent: 2em;"><samp>我是一条非常好看的评论.</samp></p><p class="text-right"><span class="glyphicon glyphicon-time"><span class="label label-info">2020/1/1 10:23:04</span></span></p></div></div><div class="panel panel-default" style="margin-top: 20px;"><div class="panel-heading"><span class="glyphicon glyphicon-user"><span class="label label-success">xxxx</span></span><p style="margin-top: 10px;text-indent: 2em;"><samp>我是一条非常好看的评论.</samp></p><p class="text-right"><span class="glyphicon glyphicon-time"><span class="label label-info">2020/1/1 10:23:04</span></span></p></div></div>
</div><form class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;margin-top: 10px;padding: 30px;"><div class="form-group"><label for="name">Name</label><input id="name" class="form-control" placeholder="用户名称" required type="text"></div><div class="form-group"><label for="email">Email</label><input id="email" class="form-control" placeholder="评论内容" required type="email"></div><button class="btn btn-default" type="submit">发布评论</button>
</form><div style="height: 50px;"></div>
</body>
</html>

三.删

 删除新闻

  点击删除按钮,跳转到该界面,进行删除新闻。

  创建一个新的jsp文件,当点击删除按钮时会跳转到该界面,跳转到界面时,要携带新闻的id过来,根据id进行删除,在判断是否删除成功。

  

<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.Connection" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%//获得新闻的idString newId = request.getParameter("newId");//根据id去数据库做删除操作//加载驱动Class.forName("oracle.jdbc.driver.OracleDriver");//定义连接字符串String url = "jdbc:oracle:thin:@localhost:1521:orcl";//获得连接Connection con = DriverManager.getConnection(url, "scott", "zking123");//查询所有的新闻数据PreparedStatement ps = con.prepareStatement("delete from jw05_news where news_id=?");//占位符的设置ps.setInt(1,Integer.parseInt(newId));//执行并获得结果 【收到影响的行数】int i = ps.executeUpdate();if(i>0){ //删除成功out.print("<script>alert('删除成功');location.href='index.jsp'</script>");}else{ //删除失败out.print("<script>alert('删除失败');history.go(-1)</script>");}%>

四.修

修改界面代码

   该界面和增加界面是一摸一样的界面,我们在阅读新闻界面中点击修改会跳转到该界面,也要携带id过来,拿到id进行查询,把新闻赋值给到输入框上面。

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><title>bootstrap</title><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="/JavaWeb05/bootstrap-3.3.7-dist/css/bootstrap.css"><script src="/JavaWeb05/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script><script src="/JavaWeb05/bootstrap-3.3.7-dist/js/bootstrap.js"></script><style>* {outline: none !important;}body,html {background: #7f8d90;}nav,.breadcrumb {border-radius: 0px !important;margin-bottom: 0px !important;}.breadcrumb {margin-bottom: 20px !important;background: #36485c;color: white;}input,select,textarea,.panel-heading {border: none !important;border-radius: 0px !important;}.breadcrumb .active{color: yellow;}</style>
</head><body><nav class="navbar navbar-default hidden-sm hidden-xs"><div class="container-fluid"><div class="navbar-header"><a class="navbar-brand" href="/news/index.html" style="font-size: 25px;">🐖</a></div><ul class="nav navbar-nav"><li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown">新闻管理<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="#">新闻发布</a></li><li class="divider"></li><li><a href="#">类别管理</a></li></ul></li></ul><ul class="nav navbar-nav navbar-right"><li><a>245@qq.com</a></li><li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li></ul></div></nav><ol class="breadcrumb"><li>您当前的位置是</li><li>新闻发布系统</li><li class="active">新闻修改</li></ol><%String newId=request.getParameter("newId");//创建一个新闻数据库,把数据库中的新闻数据,拿出来//获取驱动Class.forName("oracle.jdbc.driver.OracleDriver");//编写连接语句String url="jdbc:oracle:thin:@localhost:1521:orcl";//获得连接对象Connection con=DriverManager.getConnection(url,"scott","zking123");//编写数据库语句PreparedStatement ps=con.prepareStatement("select * from jw05_news where news_id=?");ps.setInt(1,Integer.parseInt(newId));ResultSet rs=ps.executeQuery();String title="";int topic=0;String author="";String publisher="";String content="";if(rs.next()){title=rs.getString(2);publisher=rs.getString(5);author=rs.getString(4);content=rs.getString(6);topic=rs.getInt(3);}%><form class="container" action="doUpd.jsp"><div class="panel panel-info"><!-- hidden 是输入框隐藏 --><input type="hidden" value="<%=newId%>" name="newId"><div class="panel-heading">新闻标题</div><input name="title" value="<%=title%>" class="form-control" maxlength="50" required placeholder="标题控制在30个字之内哦~~~"><div class="panel-heading">新闻类别</div><select name="topic" class=" form-control"><%ps=con.prepareStatement("select * from jw05_topic");rs=ps.executeQuery();while(rs.next()){%><option<%=topic==rs.getInt(1)?"selected":""%>value="<%=rs.getInt(1)%>"><%=rs.getString(2)%></option><%}%></select><div class="panel-heading">新闻作者</div><input name="author" value="<%=author%>" class="form-control" maxlength="10" required placeholder="名字控制在10个字之内哦~~~"><div class="panel-heading">发布时间</div><input name="publisher" value="<%=publisher%>" type="date" value="2020/1/1" class="form-control" required placeholder="名字控制在10个字之内哦~~~"><div class="panel-heading">新闻内容</div><textarea name="content" class="form-control" rows="10" required placeholder="🙅‍达咩~~~~这是必填的"><%=content%></textarea><div class="panel-footer"><button class="btn btn-primary">修改</button><button type="reset" class="btn btn-danger">取消</button></div></div></form></body></html>

修改数据

  从修改界面跳转到这里来,拿到修改界面的数据,连接数据库修改,在判断是否修改成功。

<%@page import="java.util.Date"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%request.setCharacterEncoding("utf-8");//获取到新闻的所有信息String newId = request.getParameter("newId");String author = request.getParameter("author");String publisher = request.getParameter("publisher");String content = request.getParameter("content");String topic = request.getParameter("topic");String title = request.getParameter("title");//获取驱动Class.forName("oracle.jdbc.driver.OracleDriver");
//编写连接语句String url="jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接对象
Connection con=DriverManager.getConnection(url,"scott","zking123");
//编写数据库语句
PreparedStatement ps=con.prepareStatement("update jw05_news set news_title=?,news_topic=?,news_author=?,news_publisher=?, news_content=? where news_id=?");
//占位符的设置
ps.setString(1,title);
ps.setInt(2,Integer.parseInt(topic));
ps.setString(3,author);
ps.setString(4,new Date()+"");
ps.setString(5,content);
ps.setInt(6,Integer.parseInt(newId));
//执行并获得结果 【收到影响的行数】
int i = ps.executeUpdate();if(i>0){ //修改成功out.print("<script>alert('修改成功');location.href='index.jsp'</script>");
}else{ //修改失败out.print("<script>alert('修改失败');history.go(-1)</script>");
}
%>

五.数据库关系图 

 涉及到的一些表格,大家跟着该图片创建表。

 

  今天的学习就到这里啦,大家可以把代码复制过去看看,只要有html基础会数据库和Java的人来说,这个只不过是结合了这三种而已,也是非常简单的,大家有什么 不懂的在下方评论。 


http://chatgpt.dhexx.cn/article/WnyU16fx.shtml

相关文章

jsp登录注册代码(增删改查+网页+数据库)

目录 一登录注册代码以及效果 doregister.jsp:注册信息弹框 login.jsp:登录 dologin.jsp&#xff1a;与数据库相连、存放登陆的用户 index.jsp:主界面 update.jsp:修改 doup.jsp:修改页面&#xff08;帮助&#xff09; info.jsp&#xff1a;详情 dodel.jsp:删除界面 …

采集网页数据保存到MYSQL数据库

一、直接采集一个本地网页文件 1.创建一个网页文件date413.html(注&#xff1a;文件的路径后面还会用到) 2.网页文件date413.html中的内容为&#xff1a; <html> <head><title>搜索指数</title> </head> <body><table><tr>&…

用简单,易懂的方法将数据库和网页连接起来(例:登陆注册页面)

PHP技术&#xff0c;远比用java写代码连接数据库简单得多&#xff0c;下面看我的详细介绍&#xff1a; 1.首先要下载&#xff1a;phpstudy_pro&#xff08;关注我可以免费下载哦&#xff01;&#xff09; 2.然后打开phpstudy_pro&#xff0c;启动Apache服务器&#xff08;每次…

网页展示数据库内容

目的&#xff1a;写一个JSP访问Access数据库的user表&#xff0c;将所有的记录显示出来&#xff1b;ODBC数据源名为test&#xff0c;驱动类名为&#xff1a;“driverClassNamecom.mysql.jdbc.Driverr”&#xff0c;连接数据库的url为&#xff1a;”urljdbc:mysql://localhost:3…

[VS]网页连接数据库

工具&#xff1a; 1、Visual Studio(我使用的是vs2019) 2、SQL server(我使用的是sql2008) 3、网页运行框架&#xff1a;.Net2.0 目录 一、网站以windows身份验证连接数据库 二、网站以sql server身份验证连接数据库 三、使用工具Visual Studio中自带的连接数据库功能 一…

网页开发(十四)—数据库管理

书接上回&#xff1a; 7、MySQL指令 MySQL普通认知数据库文件夹数据表文件&#xff08;Excel文件&#xff09; 7.1 数据库管理&#xff08;文件夹&#xff09; &#xff08;1&#xff09;查看已有的数据库&#xff08;文件夹&#xff09;&#xff1a;show databases; &…

每周一品 · 音圈电机(VCM)中的磁性材料

音圈电机 (Voice coil motor, VCM) 是一种特殊形式的直驱电机&#xff0c;能将电能直接转化为直线运动机械能&#xff0c;其原理是在均匀气隙磁场中放入一个圆筒绕组&#xff0c;绕组通电产生磁场&#xff0c;带动负载设备做直线往复运动&#xff0c;改变电流的强弱和流向&…

VCM音圈马达的一些特点

VCM(Voice Coil Motor)&#xff1a;中文名称为音圈马达&#xff0c;亦称音圈电机&#xff0c;原理是在一个固定的磁场内&#xff0c;通过改变VCM线圈电流的大小&#xff0c;来控制VCM马达移动的位置&#xff0c;从而改变镜片之间的距离来达到对焦的功能。基本上在所有手机上均有…

vcm驱动芯片原理_每周一品 · 音圈电机(VCM)中的磁性材料

音圈电机 (Voice coil motor, VCM) 是一种特殊形式的直驱电机&#xff0c;能将电能直接转化为直线运动机械能&#xff0c;其原理是在均匀气隙磁场中放入一个圆筒绕组&#xff0c;绕组通电产生磁场&#xff0c;带动负载设备做直线往复运动&#xff0c;改变电流的强弱和流向&…

Camera基础知识

物理结构 一、手机Camerade的物理结构 FPC&#xff08;flexible printed circuit&#xff09;可挠性印刷电路板Sensor图像传感器IR红外i滤波片holder基座lens镜头 通常&#xff0c;一个摄像头硬件上包括5个部分&#xff1a;外壳&#xff08;马达&#xff09;、镜头&#xff…

MPEG VCM

传统是视频编码是针对人眼视觉进行优化的&#xff0c;目的是在保持人的主观失真不增加的情况下降低码率。而现在越来越多的视频需要进行机器分析&#xff0c;而人只需要看机器分析的结果。因此MPEG成立VCM&#xff08;Video Coding for Machines&#xff09;工作组探索新的标准…

VCM基本原理和主要性能指标

VCM(Voice Coil Motor)音圈马达是一种将电能转化为机械能的装置,实现直线型及有限摆角的运动,实现运动的力来源于通电线圈在磁场中受到的作用力。VCM 的精确控制需要通过外部的驱动IC 来实现。按功能分为开环VCM,中置VCM,闭环VCM,光学防抖VCM。 VCM(Voice Coil Motor)音…

camera基础知识(1)

目录 camera module 镜头lens 光圈快门 音圈马达VCM 滤光片 image sensor: 摄像头工作原理大致如下 camera作为android系统中庞大的一个模块之一&#xff0c;camera框架包含的知识十分多&#xff0c;作为一名camera工程师&#xff0c;我们不仅要熟悉代码&#xff0c;也要…

VCM绕线机的功能参数

VCM的全称是Voice Coil Motor&#xff0c;我们也叫音圈马达和音圈电机&#xff0c;是手机中比较常用的配件&#xff0c;也广泛应用于电脑、网络摄像头、扫描仪等&#xff0c;因为原理和扬声器类似&#xff0c;所以叫做音圈电机。它与我们常见的电机不一样&#xff0c;具有结构简…

【camera】1. 相机硬件组成

相机—光照测量设备 图像—辐射能量测量 Lens:镜头 VCM:音圈马达 IR Filter:滤光片 Sensor:感光元件CCD\CMOS, Substrate PCB:基片电路 DSP(option):数字信号处理器 ISP(option):图像信号处理器 CCD 电耦合器件 CMOS 互补性金属氧化物半导体 无论是ccd还是cmos都是光…

VCM驱动IC--close loop

VCM驱动IC&#xff1a; 正从幕后走向台前 作为与VCM&#xff08;音圈马达&#xff09;匹配的driver IC一直被产业链当做一个小器件&#xff0c;故而默默无闻&#xff0c;较少为人所认知和谈论。其实小小的driver IC&#xff0c;是摄像模组实现自动对焦功能不可或缺的一部分…

vcm驱动芯片原理_技术科普 | 一文了解音圈马达的驱动原理

原标题:技术科普 | 一文了解音圈马达的驱动原理 第11期 庄子云: “鲲之大,不知其几千里也。化而为鸟,其名为鹏。鹏之背,不知其几千里也,怒而飞,其翼若垂天之云。”大鹏鸟展翅高飞借助的是风带来的动力。 可见,在一个运转的系统中,动力是多么的重要。我们的手机摄像头系…

手机相机接口介绍

原文来自公众号&#xff1a;工程师看海 相机是手机中非常重要的模组之一&#xff0c;已成为智能手机的标配&#xff0c;其按布局可以分为前摄和后摄&#xff0c;按功能可以分为自拍相机、主相机、超广角、长焦和微距等。 不同功能的相机有不同功能的结构和电气特性&#xff0c;…

音圈电机工作原理与直线电机的对比

图片来自&#xff1a;什么是音圈电机&#xff1f;如何工作的&#xff1f;如何应用于相机镜头调焦&#xff1f; - 知乎 目录 1. 什么是音圈&#xff1f; 2&#xff0e;音圈电机(VCM,Voice Coil Motor)结构及原理 2.1 音圈电机工作原理 2.2 音圈电机结构 2.3 音圈电机的特点 …

【转】VCM驱动IC--close loop

VCM驱动IC&#xff1a; 正从幕后走向台前 作为与VCM&#xff08;音圈马达&#xff09;匹配的driver IC一直被产业链当做一个小器件&#xff0c;故而默默无闻&#xff0c;较少为人所认知和谈论。其实小小的driver IC&#xff0c;是摄像模组实现自动对焦功能不可或缺的一部分…