当前位置:Gxlcms > html代码 > jsp+servlet问题100分你能都拿走么_html/css_WEB-ITnose

jsp+servlet问题100分你能都拿走么_html/css_WEB-ITnose

时间:2021-07-01 10:21:17 帮助过:14人阅读

求jsp写的修改个人信息的代码!数据库是access。
<%@page import="bean.UserinfoBean"%>
<%@page import="bean.MessageinfoBean"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
UserinfoBean userinfo=(UserinfoBean)session.getAttribute("userinfors");
%>
<%
UserinfoBean userinfoBean = (UserinfoBean)request.getSession().getAttribute("userinfoBean");
%>






------------------

下面是Java部分:

DB类[时间仓促,可改进地方很多,自己慢慢练吧]

package test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;/** * Created by prd on 2014/11/6. */public class DB {    private Connection conn = null;    /**     * 获取连接     *     * @return     * @throws java.sql.SQLException     */    public Connection getConn() {        try {            Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/person", "root", "123456");            return conn;        } catch (Exception e) {            e.printStackTrace();        }        return conn;    }    /**     * 关闭连接     *     * @throws SQLException     */    public void closeConn() {        try {            if (conn != null && conn.isClosed()) {                conn.close();            }        } catch (SQLException e) {            e.printStackTrace();        }    }    /**     * 更新数据.     * 

* 数组分别是:username,name,sex,email, * id为固定.可动态修改. * * @param params * @return */ public int update(String[] params) { conn = getConn(); String sql = " update student set username='" + params[0] + "',name='" + params[1] + "',sex='" + params[2] + "',email='" + params[3] + "' where id=1 "; Statement st = null; int count = 0; try { st = conn.createStatement(); count = st.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } closeConn(); return count; }}



下面是servlet类:

package test;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/** * Created by prd on 2014/11/6. */public class UpdateUserServlet extends HttpServlet {    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        request.setCharacterEncoding("utf-8");        String name = request.getParameter("name");        String username = request.getParameter("username");        String sex = request.getParameter("sex");        String email = request.getParameter("email");        if(sex.equals("0"))        {            sex="男";        }else        {            sex="女";        }        DB db = new DB();        int count =db.update(new String[]{username,name,sex,email});        if(count>0)        {            System.out.println("更新成功");        }else        {            System.out.println("更新失败");        }    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    }}


下面是jsp页面[ 替换你原来的表单部分]:

  

下面是web.xml的配置:

            UpdateUserServlet        test.UpdateUserServlet                UpdateUserServlet        /servlet/UpdateUserServlet    


记得一定要导入mysql的驱动包,直接百度搜索java连接mysql驱动包就可以,加入到项目里面。

如果你要用access,改掉这两句就可以了.

 Class.forName("com.mysql.jdbc.Driver");// 加载Access数据的驱动            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/person", "root", "123456");//连接地址

sql code这个放到那里,表已经有了,叫userinfo,而且那个db 最下面的更新数据我怎么加到我的db里面啊

大神,完全跟你写的不一样,我已经打私信给你了,你打开加我qq远成协助一下你就知道怎么回事了

人气教程排行