JDBC实现写入成绩到数据库

关注观看!

小型例题:

 

 

 

创建数据库,和表,初始化为:

1.创建第一个jsp页面查询数据库中的内容显示到页面上,创建form.jsp页面,代码如下:

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'form.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <p style="font-size:35px; " align="center">学生成绩输入页面</p>
  <div style="border:solid 2px black; height:250px;width:600px;margin:0 auto;">
    
    <form action="update.jsp" method="post">
    
    <table align="center">
             
    <tr bgcolor="yellow"> 
    <span style="padding-left:150px;">输入学生成绩:</span>
    <th>学号</th>
    <th>姓名</th>
    <th>考试类型</th>
    <th>分数</th>
    </tr>    
    <%
    Class.forName("com.mysql.jdbc.Driver");//加载驱动
    Connection con=DriverManager.getConnection("jdbc:mysql:///user", "root", "123456");//连接数据库
    String s1="select * from stu";
    Statement st=con.createStatement();//创建Statement对象,执行SQL语句
    ResultSet rs=st.executeQuery(s1);//
    while(rs.next()){
    String id=rs.getString(1);
    String name=rs.getString(2);
    String type=rs.getString(3);
    int score=rs.getInt(4);%>
    <tr bgcolor="pink">
    <td><%=id %></td>
    <td><%=name %></td>
    <td><%=type %></td>
    <td>
    <%
    if(score==0){%>
    <input name="score" type="text">
        <input name="id" type="hidden" value="<%=id%>">
    <%}else{
    out.print(score);    
    }
     %>   
    </td>
    </tr>
    <input type="submit" value="提交成绩" style="position:absolute;top:250px;left:400px;">
    <input type="button" value="返回主页面" style="position:absolute;top:250px;left:500px;">
    <%}
     %>
    
    </table>
    </form>
 </div> 
  </body>

</html>

2.创建更新数据页面,update.jsp页面,代码如下:

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'update.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
    request.setCharacterEncoding("utf-8");
    String[] score=request.getParameterValues("score");//利用数据接收,成绩不只一个
    String[] id=request.getParameterValues("id");
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql:///user", "root", "123456");
    String s1="update stu set score=? where id=?";
    PreparedStatement ps=con.prepareStatement(s1);
    
    for(int i=0;i<id.length;i++){
        if(!score[i].equals("")){
           ps.setString (1, score[i]);  
           ps.setString(2, id[i]);
           ps.executeUpdate();
        }
    }
    
        
    ps.close();
    con.close();   //关闭连接 
     %>
     <jsp:forward page="form.jsp"></jsp:forward>
  </body>
</html>

结果:

 

输入成绩:

 

点击提交成绩:

 

 

 数据库中成绩变化:

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值