zoukankan      html  css  js  c++  java
  • JSP--TOMCAT-MYSQL web页面添加

    addStudent.jsp如下
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=gb2312" %> <% 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> <title>Add info </title> </head> <body> This is my JSP page. <br> <table style="border-right:#89d4f8 1px solid; border-top:#89d4f8 1px solid; border-left:#89d4f8 1px solid" cellSpacing=0 cellpadding=70 align=center bgColor=#ffffff border=0> <tbody> <tr> <td height=26>&nbsp;&nbsp;添加学生信息</td> </tr> <tr> <td height=1 bgColor=#89d4f8> </td> </tr> </tbody> </table> <!--以get形式提交窗体数据 --> <form name=messages method="get" action="after_addStudent.jsp"> <table width="551" align="center" border="0"> <tr> <td width="167">学号:</td> <td><input type="text" name="StuID"></input> </td> </tr> <tr> <td width="167">姓名:</td> <td><input type="text" name="StuName"></input></td> </tr> <tr> <td width="167">联系电话:</td> <td><input type="text" name="Telephone"></input></td> </tr> <tr> <td width="67"></td> <td><input type="submit" value="添加"><font size="2"></font> <input type="reset"> </td> </tr> </table> </form> </body> </html>

    after_addStudent.jsp页面代码

    <%@ page language="java" 
    import="java.util.*" 
    import="com.mysql.jdbc.Driver" 
    import="java.sql.*" 
    contentType="text/html;charset=gb2312" 
    pageEncoding="UTF-8"%>
    <!--导入相关的类 ,规定编码gb2312-->
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    
    <title>Add Result</title>
    </head>
    <body>
    <%
          Connection conn;
           Statement stat;
           //设置连接的url,其中student是数据库名称
           String url="jdbc:mysql://localhost:3306/student";
           //我使用的是免安装版的mysql,用户:root,密码:zhangweijie
           String userName="root";
           String password="zhangweijie";
           //获取addStudent.jsp提交的窗体中的数据 
           int StuID=Integer.parseInt(request.getParameter("StuID"));//学号是整形
           String StuName=new String(request.getParameter  //姓名是字符串形式
           ("StuName").getBytes("iso-8859-1"),"gb2312");
           
           String Telephone=new String(request.getParameter //电话是字符串形式
           ("Telephone").getBytes("iso-8859-1"),"gb2312");
           
           
           
           try{
           //注册JDBC驱动程序
              Class.forName("com.mysql.jdbc.Driver");
           
           }
           catch(ClassNotFoundException ex){
           
               out.println("找不到驱动程序!");
           }
           //打开数据库连接
           conn=DriverManager.getConnection(url, userName, password);
          
           try{
                stat = conn.createStatement();
                 //执行插入操作
                stat.executeUpdate("insert into Student_Info values("+StuID+",'"+StuName+"','"+Telephone+"')");
                out.println("数据表操作成功!已添加学生:"+StuName+"的信息");
    
              }  
          
           
           catch(SQLException ex)
           {
              //再次刷新就会提示如下,原因是Student_Info数据表中的数据已经存在,
             //再次刷新页面相当于重新把上面的数据添加到数据表中,由于原来已经存在,故提示如下
                out.println("数据表操作失败!");
           }
        
          finally{
              //关闭数据库连接
               conn.close();
          }
    
     %>
    </body>
    </html>
  • 相关阅读:
    vue 中的虚拟dom
    Vue基操
    表头固定,表的主体设置滚动条,同时解决错位问题
    AngularJS处理服务器端返回的JSON数据的格式问题
    jQuery ajax-param()
    Bootstrap中内联单选按钮
    angularJS中控制器和作用范围
    如何理解MVC?
    CSS3动画简介以及动画库animate.css的使用
    UGUI实现打字的效果
  • 原文地址:https://www.cnblogs.com/yuhuameng/p/3750140.html
Copyright © 2011-2022 走看看