1. 程式人生 > >java web實踐

java web實踐

  • 語言:java、javascript
  • 軟體:eclipse、mysql

  環境配置:下載jdk;配置jdk環境變數。相關教程:https://jingyan.baidu.com/article/db55b609fa946e4ba20a2f56.html

  配置Tomcat、以及mysql的安裝,jdbc的下載。

  編寫一個網頁完成課程的增刪改查,要求連線資料庫並且實現增刪改查。

  首先建立一個java web專案File->new->Other->Dynamic Web Project

建立名稱->點選next

點選next,將選項打上對號:

點選完成:

 

jdbc的連結:下載jdbc。在專案檔案位置建立lib資料夾將下載jdbc中的貼上到lib資料夾下。

在專案上點選右鍵選擇最後一個Properties(屬性)選項。

 

 

選擇java build path中的Libraries

點選右側的ARR JARs...選擇剛才貼上的檔案。最後點選Apply and Close

出現Referenced Libraries證明新增成功。

在WebContent上點選右鍵new->jsp檔案:建立多個jsp檔案實現網頁的實現。在java Resources中建立java專案連結編寫資料庫的程式碼並封裝成類。

 原始碼:

Shujuku.java:

 

package Class;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Shujuku {
    public String[][] Getinformation() {
         Connection con;
        
//驅動程式名 String driver = "com.mysql.jdbc.Driver"; //URL指向要訪問的資料庫名mydata String url = "jdbc:mysql://localhost:3306/class?serverTimezone=UTC"; //MySQL配置時的使用者名稱 String user = "root"; //MySQL配置時的密碼 String password = "1234567"; //遍歷查詢結果集 String [][] st; st=new String [20][3]; int i=0; try { //載入驅動程式 Class.forName(driver); //1.getConnection()方法,連線MySQL資料庫!! con = DriverManager.getConnection(url,user,password); //2.建立statement類物件,用來執行SQL語句!! Statement statement = con.createStatement(); //要執行的SQL語句 String sql = "select * from class"; //3.ResultSet類,用來存放獲取的結果集!! ResultSet rs = statement.executeQuery(sql); while(rs.next()){ st[i][0] = rs.getString("課程名稱"); st[i][1] = rs.getString("任課教師"); st[i][2] = rs.getString("任課地點"); i++; if(i==19)break; } rs.close(); con.close(); } catch(ClassNotFoundException e) { System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace(); } catch(SQLException e) { //資料庫連線失敗異常處理 e.printStackTrace(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return st; } public void add(String id,String newname,String newpwd) { Connection con; //驅動程式名 String driver = "com.mysql.jdbc.Driver"; //URL指向要訪問的資料庫名mydata String url = "jdbc:mysql://localhost:3306/class?serverTimezone=UTC"; //MySQL配置時的使用者名稱 String user = "root"; //MySQL配置時的密碼 String password = "1234567"; //遍歷查詢結果集 try { //載入驅動程式 Class.forName(driver); //1.getConnection()方法,連線MySQL資料庫!! con = DriverManager.getConnection(url,user,password); //3.ResultSet類,用來存放獲取的結果集!! PreparedStatement psql; //預處理新增資料,其中有兩個引數--“?” psql = con.prepareStatement("insert into class (課程名稱,任課教師,任課地點) " + "values(?,?,?)"); psql.setString(1, id); //設定引數1,建立id為3212的資料 psql.setString(2, newname); //設定引數2,name 為王剛 psql.setString(3, newpwd); psql.executeUpdate(); //執行更新 con.close(); } catch(ClassNotFoundException e) { System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace(); } catch(SQLException e) { //資料庫連線失敗異常處理 e.printStackTrace(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public void update(String id,String newname,String newteachername,String newclassadd) { Connection con; //驅動程式名 String driver = "com.mysql.jdbc.Driver"; //URL指向要訪問的資料庫名mydata String url = "jdbc:mysql://localhost:3306/class?serverTimezone=UTC"; //MySQL配置時的使用者名稱 String user = "root"; //MySQL配置時的密碼 String password = "1234567"; //遍歷查詢結果集 try { //載入驅動程式 Class.forName(driver); //1.getConnection()方法,連線MySQL資料庫!! con = DriverManager.getConnection(url,user,password); PreparedStatement psql; //預處理更新(修改)資料,將王剛的sal改為5000.0 psql = con.prepareStatement("update class set 課程名稱 = ? , 任課教師=?, 任課地點=? where 課程名稱 = ?"); psql.setString(1,newname); psql.setString(2,newteachername); psql.setString(3,newclassadd); psql.setString(4,id); psql.executeUpdate(); con.close(); } catch(ClassNotFoundException e) { System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace(); } catch(SQLException e) { //資料庫連線失敗異常處理 e.printStackTrace(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public void delte(String id) { Connection con; //驅動程式名 String driver = "com.mysql.jdbc.Driver"; //URL指向要訪問的資料庫名mydata String url = "jdbc:mysql://localhost:3306/class?serverTimezone=UTC"; //MySQL配置時的使用者名稱 String user = "root"; //MySQL配置時的密碼 String password = "1234567"; //遍歷查詢結果集 try { //載入驅動程式 Class.forName(driver); //1.getConnection()方法,連線MySQL資料庫!! con = DriverManager.getConnection(url,user,password); //3.ResultSet類,用來存放獲取的結果集!! PreparedStatement psql; //預處理刪除資料 psql = con.prepareStatement("delete from class where 課程名稱 =?"); psql.setString(1, id); psql.executeUpdate(); psql.close(); con.close(); } catch(ClassNotFoundException e) { System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace(); } catch(SQLException e) { //資料庫連線失敗異常處理 e.printStackTrace(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }

xuanke.jsp:

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>選課網站</title>
<style>
nav{ width:100%; height:20px; background:#DCDCDC ; text-align:center;}
a {    text-decoation:none;}
</style>
</head>
<body>
<nav>
<p align="center">石家莊鐵道大學課程管理系統</p>
</nav>
<div id=Layer1 style="position:absolute; ;z-index:1;left: 0px;top:50px;">
<table border="1"  >
<tr bgcolor="#DCDCDC"><td></td></tr>
<tr><td align="center" bgcolor=Aqua>課程管理</td></tr>
<tr><td align="center"> <a href="Addclass.jsp">新增課程</a></td></tr>
<tr><td align="center"> <a href="Showclass.jsp">檢視課程資訊</a></td></tr>
<tr><td align="center"> <a href="Searchclass.jsp">查詢課程資訊</a></td></tr>
</table></div>
</body>
</html>

 

 addClass.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新增課程</title>
</head>
<body>
<nav>
<p align="center">石家莊鐵道大學課程管理系統</p>
</nav>
<form action="Add.jsp" method="post">
<div>
<table style="margin:0 auto">
    <tr><td style="width:120px"align="right">課程名稱:</td>
    <td> <input  style="width:80px;height:17px;" type="text" name="classname"id="classname"/></td></tr>
    <tr><td style="width:120px"align="right">任課教師:</td>
    <td> <input  style="width:80px;height:17px;" type="text" name="teachername"id="teachername"/></td>
    <td style="width:120px"align="left"><small>從王建民、劉立嘉、劉丹、楊子光、王輝中選擇。:</small></td></tr>
    <tr><td style="width:120px"align="right">任課地點:</td>
    <td> <input  style="width:80px;height:17px;" type="text" name="classadd"id="classadd"/></td></tr>
    <tr><td><button id="anniu"  style="width:100px;height:30px;margin:0 auto" type="submit" >確認</button></td>
    <td><button id="anniu"  style="width:100px;height:30px;margin:0 auto" type="button" onclick="javascript:back();">取消</button></td></tr>
</table>
</div>
</form>
<script type="text/javascript">
function back(){
    window.location.href="Xuanke.jsp?act=Xuanke"
}
</script>
</body>
</html>

 

 add,jsp:

<%@ page import="javax.swing.*" %>
<%@page import="Class.Shujuku"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新增課程</title>
<style>
nav{ width:100%; height:20px; background:#DCDCDC ; text-align:center;}
a {    text-decoation:none;}
</style>
</head>
<body>
<nav>
<p align="center">石家莊鐵道大學課程管理系統</p>
</nav>
<%
request.setCharacterEncoding("UTF-8");
Shujuku shu=new Shujuku();
String newname=request.getParameter("classname");
String newpassword=request.getParameter("teachername");
String classaddname=request.getParameter("classadd");
String st[][];
st=shu.Getinformation();
int i=0;
boolean m=true,n=false,q=true;
for(;st[i][0]!=null;i++){
    if(newname.equals(st[i][0])){m=false;break;}
}
if(newpassword.equals("王建民")||newpassword.equals("劉丹")||newpassword.equals("劉立嘉")||newpassword.equals("王輝")||newpassword.equals("楊子光"))n=true;
if((classaddname.indexOf("基教")==-1)&&(classaddname.indexOf("一教")==-1)&&(classaddname.indexOf("二教")==-1)&&(classaddname.indexOf("三教")==-1))q=false;
if(m==true&&n==true&&q==true){
    shu.add(newname, newpassword, classaddname);
%>
<script type="text/javascript">
alert("新增成功");
window.location.href="Xuanke.jsp?act=Xuanke"
</script>
<%}else{if(m==false){ %>
<script type="text/javascript">
alert("該課程已存在");
window.location.href="Xuanke.jsp?act=Xuanke"
</script>
<%} %>
<script type="text/javascript">
alert("輸入格式不正確");
window.location.href="Addclass.jsp?act=Addclass"
</script>
<%} %>
</body>
</html>

ChangeClass.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改課程資訊</title>
<style>
nav{ width:100%; height:20px; background:#DCDCDC ; text-align:center;}
</style>
<script>
function back(){
    window.location.href="Change.jsp?act=Change"
}
</script>
</head>
<body>
<nav>
<p align="center">石家莊鐵道大學課程管理系統</p>
</nav>
<form action="Change.jsp">
<div>
<table align="center">
<tr><td style="width:200px"align="right">請輸入課程名稱:</td>
    <td> <input  style="width:100px;height:20px;" type="text" name="classname"id="classname"/></td>
</tr>
<tr>
    <td style="width:200px"align="right">請輸入新的課程名稱:</td>
    <td> <input  style="width:100px;height:20px;" type="text" name="newclassname"id="newclassname"/></td>
</tr>
<tr>
    <td style="width:200px"align="right">請輸入新的任課教師:</td>
    <td> <input  style="width:100px;height:20px;" type="text" name="newteachername"id="newteachername"/></td>
</tr>
<tr>
    <td style="width:200px"align="right">請輸入新的任課地點:</td>
    <td> <input  style="width:100px;height:20px;" type="text" name="newclassaddname"id="newclassaddname"/></td>
</tr>
<tr>
    <td><button id="anniu"  style="width:100px;height:30px;margin:0 auto" type="submit" >確認</button></td>
    <td><button id="anniu2"  style="width:100px;height:30px;margin:0 auto" type="submit" onclick="jscripet:back();">取消</button></td>
</tr>
</table>
</div>
</form>
</body>
</html>

Change.jsp:

<%@ page import="javax.swing.*" %>
<%@page import="Class.Shujuku"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改課程資訊</title>
<style>
nav{ width:100%; height:20px; background:#DCDCDC ; text-align:center;}
</style>
</head>
<body>
<nav>
<p align="center">石家莊鐵道大學課程管理系統</p>
</nav>
<% 
    request.setCharacterEncoding("UTF-8");
    Shujuku shu=new Shujuku();
    String classname=request.getParameter("classname");
    String newclassname=request.getParameter("newclassname");
    String teachername=request.getParameter("newteachername");
    String classaddname=request.getParameter("newclassaddname");
    String st[][];
    st=shu.Getinformation();
    int i=0;
    boolean m=true,n=false,q=true;
    for(;st[i][0]!=null;i++){
        if(!(classname.equals(st[i][0]))&&newclassname.equals(st[i][0])){m=false;break;}
    }
    if(teachername.equals("王建民")||teachername.equals("劉丹")||teachername.equals("劉立嘉")||teachername.equals("王輝")||teachername.equals("楊子光"))n=true;
    if((classaddname.indexOf("基教")==-1)&&(classaddname.indexOf("一教")==-1)&&(classaddname.indexOf("二教")==-1)&&(classaddname.indexOf("三教")==-1))q=false;
    if(m==true&&n==true&&q==true){
    shu.update(classname, newclassname,teachername,classaddname);
%>
<script type="text/javascript">
    alert("修改成功");
    window.location.href="Showclass.jsp?act=Showclass"
</script>
<%}else{if(m==false){ %>
<script type="text/javascript">
alert("該課程已存在");
window.location.href="Changeclass.jsp?act=Changeclass"
</script>
<%} %>
<script type="text/javascript">
alert("輸入格式不正確");
window.location.href="Changeclass.jsp?act=Changeclass"
</script>
<%} %>
</body>
</html>

DelteClass.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>刪除課程資訊</title>
<style>
nav{ width:100%; height:20px; background:#DCDCDC ; text-align:center;}
</style>
<script>
function back(){
    window.location.href="Change.jsp?act=Change"
}
</script>
</head>
<body>
<nav>
<p align="center">石家莊鐵道大學課程管理系統</p>
</nav>
<form action="Delte.jsp">
<div>
<table align="center">
<tr><td style="width:200px"align="right">請輸入要刪除的課程名稱:</td>
    <td> <input  style="width:100px;height:20px;" type="text" name="classname"id="classname"/></td>
</tr>
<tr>
    <td><button id="anniu"  style="width:100px;height:30px;margin:0 auto" type="submit" >確認</button></td>
    <td><button id="anniu2"  style="width:100px;height:30px;margin:0 auto" type="submit" onclick="jscripet:back();">取消</button></td>
</tr>
</table>
</div>
</form>
</body>
</html>

Delte.jsp:

<%@ page import="javax.swing.*" %>
<%@page import="Class.Shujuku"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改課程資訊</title>
<style>
nav{ width:100%; height:20px; background:#DCDCDC ; text-align:center;}
</style>
</head>
<body>
<nav>
<p align="center">石家莊鐵道大學課程管理系統</p>
</nav>
<% 
    request.setCharacterEncoding("UTF-8");
    Shujuku shu=new Shujuku();
    String classname=request.getParameter("classname");
    shu.delte(classname);
%>
<script type="text/javascript">
    alert("刪除成功");
    window.location.href="Showclass.jsp?act=Showclass"
</script>
</body>
</html>

Searchclass.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查詢課程</title>
<style>
nav{ width:100%; height:20px; background:#DCDCDC ; text-align:center;}
a {    text-decoation:none;}
</style>
</head>
<body>
<nav>
<p align="center">石家莊鐵道大學課程管理系統</p>
</nav>
<div>
    <form action="Sharch.jsp">
        <table border="1" align="center">
        <tr><td style="width:120px"align="right">要查詢的內容:</td>
        <td> <input  style="width:80px;height:17px;" type="text" name="name" id="name"/></td></tr>
        <tr><td align="center"><button id="anniu"  style="width:100px;height:30px;margin:0 auto" type="submit" >確認</button></td>
        <td align="center"><button id="anniu"  style="width:100px;height:30px;margin:0 auto" type="button" onclick="javascript:back();">取消</button></td></tr>
        </table>
    </form>
</div>
<script>
function back(){
    window.location.href="Xuanke.jsp?act=Xuanke"    
}
</script>
</body>
</html>

Seacher.jsp:

<%@ page import="javax.swing.*" %>
<%@page import="Class.Shujuku"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查詢課程</title>
<style>
nav{ width:100%; height:20px; background:#DCDCDC ; text-align:center;}
a {    text-decoation:none;}
</style>
</head>
<body>
<nav>
<p align="center">石家莊鐵道大學課程管理系統</p>
</nav>
<%
request.setCharacterEncoding("UTF-8");
Shujuku shu=new Shujuku();
String newname=request.getParameter("name");
String [] []st;
st=shu.Getinformation();
String [][]str;
str=new String [20][3];
for(int i=0,j=0;st[i][0]!=null;i++){
    if(st[i][0].indexOf(newname)!=-1){str[j]=st[i];j++;continue;}
    if(st[i][1].indexOf(newname)!=-1){str[j]=st[i];j++;continue;}
    if(st[i][2].indexOf(newname)!=-1){str[j]=st[i];j++;continue;}
}
%>
<div>
<table border="1" align="center">
<thead>
    <tr>
        <th align="center">課程名稱</th>
        <th align="center">任課教師</th>
        <th align="center">任課地點</th>
    </tr>
</thead>

    <%int i=0;
    while(str[i][0]!=null){%>
    <tr>
        <td align="center"><%out.print(str[i][0]); %></td>
        <td align="center"><%out.print(str[i][1]); %></td>
        <td align="center"><%out.print(str[i][2]); %></td>
</tr>
<%i++;} %>
    <tr align="center"><td></td>
    <td><input id="anniu"  style="width:100px;height:30px;margin:0 auto" type="button" onclick="javascript:back();"value="確認"></td>
    <td></td></tr>
</table>
</div>
<script>
function back(){
    window.location.href="Xuanke.jsp?act=Xuanke"    
}
</script>
</body>
</html>

ShowClass.jsp:

<%@ page import="javax.swing.*" %>
<%@page import="Class.Shujuku"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>課程資訊</title>
<%
    request.setCharacterEncoding("UTF-8");
    Shujuku shu=new Shujuku();
    String [][] st;
    int i=0;
    st=shu.Getinformation();
%>
<script type="text/javascript">
function change(){
    window.location.href="Changeclass.jsp?act=Change"
}
function delte(){
    window.location.href="Delteclass.jsp?act=Delte"
}
</script>
<style>
nav{ width:100%; height:20px; background:#DCDCDC ; text-align:center;}
body{text-align:center} 
</style>
</head>
<body>
<nav>
<p align="center">石家莊鐵道大學課程管理系統</p>
</nav>
<div>
<table border="1" align="center">
<thead>
    <tr>
        <th align="center">課程名稱</th>
        <th align="center">任課教師</th>
        <th align="center">任課地點</th>
    </tr>
</thead>

    <%while(st[i][0]!=null){%>
    <tr>
        <td align="center"><%out.print(st[i][0]); %></td>
        <td align="center"><%out.print(st[i][1]); %></td>
        <td align="center"><%out.print(st[i][2]); %></td>
        <td><input id="anniu[<%=i%>]"  style="width:100px;height:30px;margin:0 auto" type="button" onclick="javascript:change();"value="修改"></td>
        <td><input id="anniu[<%=i%>]"  style="width:100px;height:30px;margin:0 auto" type="button" onclick="javascript:delte();"value="刪除"></td>

</tr>
<%i++;}%>

</table>
</div>
</body>
</html>