JSP实现HTML网页管理Mysql数据库

Posted WillWinwin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSP实现HTML网页管理Mysql数据库相关的知识,希望对你有一定的参考价值。

使用JSP实现html网页管理mysql数据库主要有以下几个地方需要注意:

  1. 使用request.getParamater获取到输入的值
  2. 对输入数值的一些逻辑判断
  3. Mysql的语法操作
  4. 编写一个好看的HTML界面
  5. you win
<%@page import="java.sql.Connection,java.sql.Statement,java.util.Scanner,java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
request.setCharacterEncoding("utf-8");
String msg="";
boolean error=false;
获取是否点击了保存按钮
String s1 = request.getParameter("save");
if(s1!=null)
初始化数据库表中的属性
    String Id = "";
    String Name = "";
    String Age = "";
    String Grade = "";
    String Hobby[]= "";
    String Hobbies = "";
    使用循环是因为想要判断是否有属性为空,如果为空就直接跳出去不用执行接下来的语句
    for(int j = 0 ; j < 1 ; j++)
        Id = "";
        Name = "";
        Age = "";
        Grade = "";
        Hobbies = "";

        if(request.getParameter("Id")!=null)
            Id = request.getParameter("Id");
            **判断Id是否为空需要按照实际情况,比如有时候是Id == null**
            if(Id == "")
                error = true;
                msg = "ID cant be empty!";
                break;
            
        

        if(request.getParameter("Name")!=null)
            Name = request.getParameter("Name");
            if(Name == "")
                error = true;
                msg = "Name cant be empty!";
                break;
            
        

        if(request.getParameter("Age")!=null)
            Age = request.getParameter("Age");
            if(Age == "")
                error = true;
                msg = "Age cant be empty!";
                break;
            
        

        if(request.getParameter("Grade")!=null)
            Grade = request.getParameter("Grade");
            if(Grade == "")
                error = true;
                msg = "Grade cant be empty!";
                break;
            
        


        if(request.getParameter("Hobby")!=null)
            Hobby = request.getParameterValues("Hobby");
            int len = Hobby.length;
            for(int i = 0 ; i < len ; i++)
                if(i==0)
                    if(len==1)
                        Hobbies = Hobby[0];
                    else
                        Hobbies = Hobbies + Hobby[i] + "+";
                    
                else if(i == (len-1))
                    Hobbies = Hobbies + Hobby[i];

                else
                    Hobbies = Hobbies + Hobby[i] + "+";
                
            
            if(Hobbies == null)
                error = true;
                msg = "Hobby cant be empty!";
                break;
            
        
    


    if(!error)
连接数据库
        try
            String connectString = "jdbc:mysql://localhost/test"
                    + "?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8";
            String user = "root";
            String pwd = "mysql";
            获取所有的数值
            String sql = "select *from stu where id = '" + Id + "';";
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(connectString, user, pwd);

            Statement state = conn.createStatement();
            ResultSet rs = state.executeQuery(sql);
            判断Id是否唯一
            if(rs.next())
                msg="The user exits!";
                error = true;
            
            if(!error)
            执行插入操作
                sql = "insert into stu values ('" + Id + "','" + Name + "','" + Age + "','"
            + Grade + "','" + Hobbies + "');";
                int cnt = state.executeUpdate(sql);
                if(cnt > 0)
                    msg = "The user is added!";
                else
                    msg = "Add Error!";
                
                rs.close();
                state.close();
                conn.close();
            

        catch (Exception e)
            e.printStackTrace();
        
    



%>    

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
.black
color:#000;

.grey
color:#b3b3b3;


 a:link,a:visiter
 color:blue;
 

#Main
margin:auto;
width:500px;
text-align: center;


</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>


<body>

<div id="Main">

<h1>新增学生记录</h1>
<form action="addStu.jsp" method="post">
学号:
<input type = "text" name = "Id" >
<br />
<br />
姓名:
<input type = "text" name = "Name">
<br />
<br />
年龄:
<input type = "text" name = "Age">
<br />
<br />
请选择你的年级:
<br />
<select name="Grade">
<option value="freshman">大学一年级</option>
<option value="shophomore">大学二年级</option>
<option value="junior" selected="selected">大学三年级</option>
<option value="senior">大学四年级</option>
</select>
<br />
<br />
请选择你的爱好:
<br />
<input type="checkbox" name="Hobby" value="sports">
sports
<input type="checkbox" name="Hobby" value="travel">
travel
<input type="checkbox" name="Hobby" value="music">
music
<br />
<br />
<br />
<br />
<input type = "submit" name = "save" value = "保存">
<br />
<br />
<br />
<br />
</form>

将结果的信息输出来
<p><%=msg %></p>
<br />
<br />

<a href= 'browseStu.jsp?pgno=1&pgcnt=4'>返回</a>
</div>

</body>
</html>

示例:


上传成功:

以上是关于JSP实现HTML网页管理Mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章

JSP实现HTML网页对Mysql数据库的数据修改

招新系统(jsp+servlet,实现简略前端网页注册登录+后台增删改查,分学生和管理员,Java语言,mysql数据库连接,tomcat服务器)

Java+JSP+Mysql+Tomcat实现Web用户信息管理系统

IDEA+Java+SSM+JSP+Mysql实现Web图书管理系统

IDEA+Java+SSM+JSP+Mysql实现超市订单系统

IDEA+Java+JSP+Mysql+Tomcat实现Web教师信息管理系统