当前位置: 首页> 财经> 产业 > jsp实验18 JDBC

jsp实验18 JDBC

时间:2025/7/9 1:03:35来源:https://blog.csdn.net/m0_64798714/article/details/139332313 浏览次数:0次

源代码以及执行结果截图:

admoinStudent.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<%@page import="java.sql.*"%>

<%@ page import="javax.sql.DataSource" %>

<%@ page import="javax.naming.Context" %>

<%@ page import="javax.naming.InitialContext" %>

<style>

   #tom{

      font-family:宋体;font-size:28;color:black

   }

</style>

<%  request.setCharacterEncoding("utf-8");

    Connection con=null;

    PreparedStatement pre=null;

    ResultSet rs;

    Context  context =new InitialContext();

    Context  contextNeeded=(Context)context.lookup("java:comp/env");//环境命名上下文

    DataSource ds=

    (DataSource)contextNeeded.lookup("StudentConn");

    con = ds.getConnection();

    String updateSQL =

   "update scoreReport set name =?,mathScore=?,englishScore=? where id=?";

    String insertSQL ="insert into scoreReport values(?,?,?,?)";

    String deleteSQL ="delete from scoreReport where id =?";

    String querySQL = "select * from scoreReport where id =?";

    String mess = request.getParameter("submit");

    if(mess == null) mess ="";

    String id = request.getParameter("id");

    String name = request.getParameter("name");

    String math = request.getParameter("mathScore");

    String english = request.getParameter("englishScore");

    try{

      if(mess.contains("查看")){

         pre = con.prepareStatement(querySQL);

         pre.setString(1,id);

         rs = pre.executeQuery();

         if(rs.next()){

           id = rs.getString(1);

           name = rs.getString(2);

           math = rs.getString(3);

           english = rs.getString(4);

         }

      }

      else if(mess.contains("更新")){

         pre = con.prepareStatement(updateSQL);

         pre.setString(1,name);

         pre.setInt(2,Integer.parseInt(math));

         pre.setInt(3,Integer.parseInt(english));

         pre.setString(4,id);

         pre.executeUpdate();

         out.print("<h3>更新成功</h3>");

      }

      else if(mess.contains("添加")){

         pre = con.prepareStatement(insertSQL);

         pre.setString(1,id);

         pre.setString(2,name);

         pre.setInt(3,Integer.parseInt(math));

         pre.setInt(4,Integer.parseInt(english));

         pre.executeUpdate();

         out.print("<h3>添加成功</h3>");

      }

       else if(mess.contains("删除")){

         pre = con.prepareStatement(deleteSQL);

         pre.setString(1,id);

         pre.executeUpdate();

         out.print("<h3>删除成功</h3>");

      }

    }

    catch(SQLException e) {

       out.print("<h1>学号不能重复");

       try{

         con.close();

       }

       catch(SQLException exp){}

    }

%>

  

<!DOCTYPE html>

<html>                                

<head> 

<meta charset="utf-8">

<title>Insert title here</title>

</head>

<body bgcolor = #ffccff>

<form action="" id=tom method=post >

输入学号查看或删除(信息):<br>

  <input type="text"   id=tom name="id" size=10 /><br>

  <input type="submit" id=tom name="submit" value="查看"/>

  <input type="submit" id=tom name="submit" value="删除"/>

</form>

<form action="" id=tom method=post>

更新(或添加)

<input type="text" id=tom name="id" value =<%=id%> size=9 />

学号的信息:

<br>姓名:

<input type="text" id=tom name="name" value ='<%=name%>'size=11/>

<br>数学成绩:

<input type="text" id=tom name="mathScore" value ='<%=math%>'size=7 />

<br>英语成绩:

<input type="text" id=tom name="englishScore" value ='<%=english%>'size=7/>

<br><input type="submit" id=tom name="submit" value="更新"/>

    <input type="submit" id=tom name="submit" value="添加"/>

</form>

</body>

</html>

      context.xml

<?xml version="1.0" encoding="UTF-8"?>

<Context>

   <Resource

      name="StudentConn"

      type="javax.sql.DataSource"

      driverClassName="com.mysql.cj.jdbc.Driver"

      url="jdbc:mysql://127.0.0.1:3306/stdent?

      serverTimezone=CST&amp;characterEncoding=utf8"

       username ="root"

      password ="root"

      maxActive ="5"

      maxIdle ="5"

      minIdle ="1"

      maxWait ="5000"

   />

</Context>

效果图

关键字:jsp实验18 JDBC

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: