mysql+tomcat+jsp增删改查(二)

来源:互联网 发布:中国2015gdp数据 编辑:程序博客网 时间:2024/06/11 15:04

四、创建数据库

 

mysql>create database jsp;    

mysql>use jsp

mysql>create table student(  

->id int(30) not null primary key,  

->name varchar(50),  

->age int(30),  

->gender varchar(30),  

->major varchar(50)  

->);  

 

至此创建了名为jsp的数据库,一个名叫student的表.

 

 

五、页面制作

 

 

在安装完成的Tomcat6.0中的webapps中新建一个文件夹normal

 

新建一个picWEB-INF文件夹,WEB-INF下新建一个web.xml和新建一个lib文件夹

 

pic 文件夹中放的是图片,网页要用背景图片.自己可以到网上找找。

 

不要太大的。

 

但是名字要改成background.jpg


web.xml中的内容为:

 

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

<web-app version="2.5"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <display-name></display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

 

addStuInfo.jsp

 

<%@ page language="java" import="java.util.*" 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>输入学生信息界面</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">

-->

    <script type="text/javascript"">

    function validate()// 功能验证

    {

    var id = document.forms[0].id.value;

    var name = document.forms[0].name.value;// 0id号。

    var age = document.forms[0].age.value;

    var major = document.forms[0].major.value;

    if(id <= 0){

     alert("学号不能为空,请输入学号!");

     return false;

    }

    else if(name.length <= 0){

     alert("姓名不能为空,请输入姓名!");

     return false;

    }

    else if(age <= 0){

     alert("请输入合法年龄!");

     return false;

    }

 

        else if(major.length <= 0){

     alert("专业不能为空,请输入所学专业!");

     return false;

    }

 

    else{

     return true;

    }

     //document.getElementById("form").submit();

    }

    </script>

  </head>

  

  <body background="pic/background.jpg">

  <br>

  <center>

  <h2>添加学生信息</h2><hr>

 <form action="insert.jsp" method="post" id="form" onSubmit="return validate()" >

<h4>  学号:<input type="text" name="id" class="{required:true}"></input><br></h4>

<h4>  姓名:<input type="text" name="name"></input><br></h4>

<h4>  年龄:<input type="text" name="age"></input><br></h4>

<h4>  性别:<input type="radio" name="gender" value="">

       <input type="radio" name="gender" value=""><br></h4>

<h4>  专业:<input type="text" name="major"></input><br></h4>

 <input type="submit" value="提交"/>

  </form>

  <a href="showInfo.jsp">查询所有学生信息</a>

  </center>

  </body>

</html>

 

delete.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

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

<%

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>删除页面</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 background="pic/background.jpg">

     <%

    request.setCharacterEncoding("UTF-8");

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

    Connection conn = null;

    Statement stat = null;

    ResultSet rs = null;

    Class.forName("com.mysql.jdbc.Driver");

    String url = "jdbc:mysql://localhost:3306/jsp";

    String user = "root";

    String password = "root";

    conn = DriverManager.getConnection(url,user,password);

    stat = conn.createStatement();

    stat.executeUpdate("delete from student where id = " + id + "");

    rs = stat.executeQuery("select * from student");

    

    if(rs.next())

    {

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

    }

    else{

    out.print("<center><h3>删除失败!</h3></center>");

    }

    %>

    <br>

 <br>

     <center> <a href=addStuInfo.jsp>返回添加信息页面</a> <a href=showInfo.jsp>返回信息查询页面</a></center>

      <%

    if(rs != null)

    {

        rs.close();

        rs = null;

    }

        if(stat != null)

    {

        stat.close();

        stat = null;

    }

        if(conn != null)

    {

        conn.close();

        conn = null;

    }

    %>

  </body>

</html>

 

 

index.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>

<%

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>Welcome,home</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 background="pic/background.jpg"><br/><br/>

  

   <center>

  

    <a href="addStuInfo.jsp">点此添加学生信息</a><br/><br/>

    <a href="showInfo.jsp">点此查询学生信息</a><br/><br/>

    <a href="showInfo.jsp">点此修改学生信息</a><br/><br/>

    <a href="showInfo.jsp">点此删除学生信息</a><br/><br/>

    

<br>

 

</center>

  </body>

</html>

 

 

insert.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

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

<%

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>插入学生信息</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 background="pic/background.jpg">

    <%

    request.setCharacterEncoding("UTF-8");

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

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

    System.out.println(name);

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

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

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

    Connection conn = null;

    Statement stat = null;

    ResultSet rs = null;

    Class.forName("com.mysql.jdbc.Driver");

    String url = "jdbc:mysql://localhost:3306/jsp";

    String user = "root";

    String password = "root";

    conn = DriverManager.getConnection(url, user, password);

    stat = conn.createStatement();

    String sql = "insert into student(id,name,age,gender,major) values(" + id + ",'" + name + "'," + age + ",'" + gender + "','" + major + "')";

    stat.executeUpdate(sql);

    rs = stat.executeQuery("select * from student");

%>

   

   <center>

   <%

    if(rs.next())

    {

    out.print("<br><h3>成功输入!</h3>");

    }

    else{

    out.print("<br><h3>输入失败!</h3>");

    }

  

    %>

  

   

      <br>

    <a href=addStuInfo.jsp>返回添加信息页面</a>   <a href=showInfo.jsp>进入信息查询页面</a>

    </center>

     <%

    if(rs != null)

    {

        rs.close();

        rs = null;

    }

        if(stat != null)

    {

        stat.close();

        stat = null;

    }

        if(conn != null)

    {

        conn.close();

        conn = null;

    }

    %>     

      </body>

</html>

 楼主qq:496056171

0 0