1. 程式人生 > >程式碼例項

程式碼例項

Test.java

package Ke;
import  Ke.util;
import java.sql.*;
import java.util.Scanner;
public class Test {
    static Connection conn;
    static PreparedStatement ps = null;
    static ResultSet rs;
    static String sql = "select * from Ke_c";
    static util ut= new util();
    static Scanner in = new
Scanner(System.in); static String Ke; static String Tea; static String Di; public static String a_Ke() { return Ke; } public static String a_Tea() { return Tea; } public static String a_Di() { return Di; } public static int add(String Ke,String Tea,String Di) { conn
= ut.getConn(); String sql="insert into Ke_c values(?,?,?)"; int b=0; System.out.println(Ke); try { ps=conn.prepareStatement(sql); ps.setString(1,Ke); ps.setString(2, Tea); ps.setString(3, Di); int a=ps.executeUpdate(); if(a>0) { b
++; System.out.println("新增成功"); } else { System.out.println("新增失敗"); } }catch(Exception e) { e.printStackTrace(); } try { if(ps!=null)ps.close(); if(conn!=null)conn.close(); }catch(Exception e2) { e2.printStackTrace(); } return b; } public static int uqdate(String Ke,String Tea,String Di) { int b=0; conn=ut.getConn(); ps=null; sql="update Ke_c set Tea=?,Di=? where Ke=?"; try { ps=conn.prepareStatement(sql); ps.setString(1, Tea); ps.setString(2, Di); ps.setString(3,Ke); int a=ps.executeUpdate(); if(a>0) { b++; System.out.println("修改成功"); } else { System.out.println("修改失敗"); } }catch(Exception e) { e.printStackTrace(); } try { if(ps!=null)ps.close(); if(conn!=null)conn.close(); }catch(Exception e2) { e2.printStackTrace(); } return b; } public static int delete(String Ke) { int b=0; conn=ut.getConn(); ps=null; sql="delete from Ke_c where Ke=?"; try { ps=conn.prepareStatement(sql); ps.setString(1, Ke); int a=ps.executeUpdate(); if(a>0) { b++; System.out.println("刪除成功"); } else { System.out.println("刪除失敗"); } }catch(Exception e) { e.printStackTrace(); } try { if(ps!=null)ps.close(); if(conn!=null)conn.close(); }catch(Exception e2) { e2.printStackTrace(); } return b; } public static void find(String a) { conn=ut.getConn(); ps=null; ResultSet rs=null; String id; sql="select * from Ke_c where Ke=?"; try { ps=conn.prepareStatement(sql); ps.setString(1, a); rs=ps.executeQuery(); if(rs.next()) { Ke = rs.getString("Ke"); Tea = rs.getString("Tea"); Di = rs.getString("Di"); } }catch(SQLException e) { e.printStackTrace(); }finally { try { if(ps!=null)ps.close(); if(conn!=null)conn.close(); }catch(Exception e2) { e2.printStackTrace(); } } } public static void main(String[] args) { //add(); //uqdate(); //System.out.println("請輸入要刪除課程"); System.out.println("請輸入查詢課程:"); String a; Scanner scan=new Scanner(System.in); a=scan.next(); //delete(a); find(a); } }

add.jsp

<%@page import="Ke.Test"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>增加</title>
<script type="text/javascript"">

    function validate()

    {
    var Ke=document.forms[0].Ke.value;
    var Tea=document.forms[0].Tea.value;
    var Di=document.forms[0].Di.value;
    if(Ke.length<=0){

        alert("課程不能為空,請輸入課程!");

        return false;

    }

    else if(Tea.length<=0){

        alert("教師不能為空,請輸入教師姓名!");

        return false;

    }

    else if(age<=0){

        alert("地點不能為空,請輸入地點!");

        return false;

    }

 

        else if(Di.length<=0){

        alert("專業不能為空,請輸入所學專業!");

        return false;

    }

 

    else{

        return true;

    }

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

    }

    </script>
</head>
<body>
 <form action="addresult.jsp" method="post" id="form" onSubmit="return validate()" >

 <input type="hidden" name="methodName" value="0"/>

<h4>  課程:<input type="text" name="Ke"title="課程不能為空"></input><br></h4>

<h4>  任課教師:<input type="radio" name="Tea" value="Wangjianmin">王建民

            <input type="radio" name="Tea" value="liulijia">劉立嘉
            <input type="radio" name="Tea" value="liudan">劉丹
            <input type="radio" name="Tea" value="wanghui">王輝
            <input type="radio" name="Tea" value="yangziguang">楊子光
            <br></h4>
<h4>  上課地點:<input type="radio" name="Di" value="yijiao">一教
            <input type="radio" name="Di" value="erjiao">二教
            <input type="radio" name="Di" value="sanjiao">三教
            <input type="radio" name="Di" value="jijiao">基教
            <br></h4>
 <input type="submit" value="完成"/>
   </form>
 
</body>
</html>

addresult.jsp

<%@page import="Ke.Test"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新增結果</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
 if(Test.add(request.getParameter("Ke"),request.getParameter("Tea"),request.getParameter("Di"))==1)
 {
     {
            out.print("<script language = 'javascript'>alert('新增成功');</script>");
            response.setHeader("refresh", "0;url=Zhu.jsp");
            }
 }
 else{
     out.print("<script language = 'javascript'>alert('新增失敗');</script>");
     response.setHeader("refresh", "0;url=Zhu.jsp");
        //out.println("新增失敗");
 }
 %>
</body>
</html>