user表的改

ジ你是我永遠のbugグ發表於2020-12-06

user表的改 前端對使用者資訊的修改,後端將資料庫資訊更改

package user_crud;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * Servlet implementation class updateRole
 */
@WebServlet("/updateUser")
public class UpdateUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer = response.getWriter();
		
		String upDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()).toString();
		// 獲取前端傳的json資料
		 InputStreamReader insr = new InputStreamReader(request.getInputStream(),"utf-8");
	        String result = "";
	        int respInt = insr.read();
	        while(respInt!=-1) {
	            result +=(char)respInt;
	            respInt = insr.read();
	        }
	        JSONObject json = JSONObject.parseObject(result);
	       if(json !=null) {
	        	// 獲取json資料直接用json.getString
	        	String userName = json.getString("userName");
	            String nickName = json.getString("nickName");
	            String phone = json.getString("phone");
	            String gender = json.getString("gender");
	            String remark = json.getString("remark");
	            int roleId = json.getInteger("roleId");
	            int userId = json.getInteger("userId");	            
	            System.out.println("賬號:"+userName+"暱稱:"+nickName+"電話:"+phone+"性別:"+gender+"備註:"+remark+"roleId:"+roleId+"userId:"+userId);
	            // 連線資料庫
	            Connection conn = null;
	    		PreparedStatement ps = null;
	    		try {
	    			 try {
	    				Class.forName("com.mysql.jdbc.Driver");
	    			} catch (ClassNotFoundException e) {
	    				// TODO Auto-generated catch block
	    				e.printStackTrace();
	    			}
	    			// 連線資料庫	    			
	    			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/gg?useSSL=false","root","li19961102");
	    			// 建立操作物件
	    			ps = conn.prepareStatement("update user set user_name=?,nick_name=?,phone=?,gender=?,remark=?,role_id=?,user_modified='"+upDate+"' where user_id =?");
	    			
	    			ps.setString(1, userName);
	    			ps.setString(2, nickName);
	    			ps.setString(3, phone);
	    			ps.setString(4, gender);
	    			ps.setString(5, remark);
	    			ps.setInt(6, roleId);
	    			ps.setInt(7, userId);	
    				// 更新成功
	    			ps.executeUpdate();
		    				
	    			    			
		    		String success = "{'msg':'更新成功','code':0}";
					JSONObject jsonSuccess = JSON.parseObject(success);	
					response.getWriter().write(jsonSuccess.toJSONString());
	    				
	    			
	    		} catch (SQLException e) {
	    				
	    			e.printStackTrace();
	    		}finally {	    				    			
	    			if(ps !=null) {
	    				try {
	    					ps.close();
	    				} catch (SQLException e) {
	    					// TODO Auto-generated catch block
	    					e.printStackTrace();
	    				}
	    			}	    			
	    			if(conn!=null) {
	    				try {
	    					conn.close();
	    				} catch (SQLException e) {
	    					// TODO Auto-generated catch block
	    					e.printStackTrace();
	    				}
	    			}
	    		}
	    	}	
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

相關文章