jsp页面实现对数据库的修改

Posted

tags:

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

参考技术A

1、jsp是java服务端动态网页技术,主要应用于网页构建,理论上讲不应该在页面中直接连数据库。合理的做法是先构建一个java后端,然后在JAVA后端中通过jdbc连接sqlserver。

2、如果一定要在jsp页面中连数据库也是可以的。jsp中有专门的sql标签可以连接数据库进行操作,这是jstl的内容,需要导入相应的数据库驱动包。

3、具体的操作标签为:<sql:setDataSource>,<sql:query>,<sql:update>,<sql:transaction>等等,jsp的内容相对来说技术难度都不算特别高,学习起来还是比较容易的。

4、希望对你有帮助。祝你学有所得。

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

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

  1. 注意获取原始Id,并将其赋值到当前的HTML中
  2. rs.next才能获取到数值,rs不可以
  3. 增加一些个人的逻辑判断,比如Id不可以修改,任何属性为空都不能成功修改
    示例:

    当然数据库中的数据也会相应的改变
<%@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="";
String msg1="";
boolean error=false;
获取网址中传递的Id
String origId = request.getParameter("pid");
String value1 = null;
String value2 = null;
String value3 = null;

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 = '" + origId + "';";
    Class.forName("com.mysql.jdbc.Driver");

    Connection conn = DriverManager.getConnection(connectString, user, pwd);
    Statement state = conn.createStatement();
    将原始Id中的数据写入到当前的HTML中
    ResultSet rs = state.executeQuery(sql);
    while(rs.next())  注意rs是空指针,要使用rs.next才能访问到数值
        value1 = rs.getString("id");
        value2 = rs.getString("name");
        value3 = rs.getString("age");
    
    rs.close();
    state.close();
    conn.close();
catch (Exception e)
    e.printStackTrace();


String s1 = request.getParameter("sub");

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");
            if(Id == "")
                error = true;
                msg = "ID cant be empty!";
                break;
            
            if(!Id.equals(origId))
                error = true;
                msg = "ID cant be changed!";
                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 == "")
            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 = null;
            Class.forName("com.mysql.jdbc.Driver");

            Connection conn = DriverManager.getConnection(connectString, user, pwd);
            Statement state = conn.createStatement();
            sql = "update stu set name = '" + Name + "', age = '" + Age + "', grade = '" +
        Grade + "', hobby = '" + Hobbies +"';";
            if(Hobbies=="")
                msg1 = "yes";
            else
                msg1 = "no";
            

            int cnt = state.executeUpdate(sql);
            if(cnt > 0)
                msg = "Update success!";
            else
                msg = "Add Error!";
            
            state.close();
            conn.close();

        catch (Exception e)
            e.printStackTrace();
        
    
    if(msg == "Update success!")
        value1 = Id;
        value2 = Name;
        value3 = Age;
    




%>


<!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>
   <style>
      a:link,a:visited color:blue
      .container  
        margin:0 auto; 
        width:500px;  
        text-align:center;  
       
   </style>
<body>
<div class="container">
    <h1>修改学生记录</h1>
    <form action="updateStu.jsp?pid=<%=origId %>" 
          method="post" name="f">
        学号:<input name="Id" type="text" value= "<%=value1 %>" />
        <br/><br/>
        姓名:<input name="Name" type="text" value= "<%=value2 %>" />
        <br/><br/>
        年龄:<input type = "text" name = "Age" value= "<%=value3 %>" />
        <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 />
    <input type="submit" name="sub" value="修改"/>
    <input type="reset" name="rset" value="清空"/>
    <br/><br/>
    </form>
    id is required!<br/><br/>
    <p><%=msg %></p>
    <p><%=msg1 %></p>
    <a href='browseStu.jsp'>返回</a>
    </div>

</body>
</html>

以上是关于jsp页面实现对数据库的修改的主要内容,如果未能解决你的问题,请参考以下文章

急求一个jsp+ajax实现分页的例子

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

Web对数据库的增删改查(servlet+jsp+javaBean增删改查)

jsp为啥要用增删改查

jsp页面里 怎样动态修改一个按钮的背景色?

JSP 实用程序之简易页面编辑器