1. 程式人生 > >mysql+tomcat+jsp增刪改查(二)

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

四、建立資料庫

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