码迷,mamicode.com
首页 > 编程语言 > 详细

java web实践

时间:2018-12-09 14:17:03      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:request   void   url   ack   oat   mysql的安装   table   color   tab   

  • 语言: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>

 

java web实践

标签:request   void   url   ack   oat   mysql的安装   table   color   tab   

原文地址:https://www.cnblogs.com/huan-ch/p/10090598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!