user表的改
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);
}
}
相關文章
- mysql.user表的資料準確性問題MySql
- Django的User ModelDjango
- MySql 表資料的增、刪、改、查MySql
- MySQL表的增刪查改(提高篇)MySql
- MySQL表的增刪改查(基礎)MySql
- 單表增刪改查
- Django的User Model(2)Django
- App\User 替換為 App\Models\User 的問題解決!APP
- MySQL表的增刪改查(進階)下MySql
- oracle user$Oracle
- yii2接入pgSQL(查詢不到表The table does not exist: {{%user}})SQL
- PRVF-4007 : User equivalence check failed for user "grid"UIAI
- User Notification Framework 框架的使用Framework框架
- oracle 臨時表空間的增刪改查Oracle
- Git批量修改歷史commit中的user.name 和user.emailGitMITAI
- Git 修改已初始化配置過的 user.name user.emailGitAI
- 快速瞭解:user-valid和:user-invalid
- audit by user by table
- User Account Control
- Vue @user 元件Vue元件
- MyBatis-maven-User例子-根據id查詢UserMyBatisMaven
- AUTHID CURRENT_USER的注意點
- iOS 10 的推送 User Notifications FrameworkiOSFramework
- hive建立分割槽表報錯AccessControlException Permission denied: user=NONE, access=WRITE, inodeHiveExceptionNone
- 線上重定義與普通表改為分割槽表
- Auth::user() 踩坑
- LightBeter Software User Agreement
- Oracle OCP(28):USEROracle
- 14、flask-模型-models-表的操作-增刪改查Flask模型
- MySQL資料庫 ---MySQL表的增刪改查(進階)MySql資料庫
- 聯機重定義表在10g的改進
- MySQL——表的約束,資料型別,增刪查改MySql資料型別
- 什麼是 Dynatrace 裡的 User Action
- 迷之自信的Single_User Mode
- computed watcher和user watcher的區別
- locustfile中的User類和HttpUser類HTTP
- PHP 中 call_user_func 的使用PHP
- 神祕的.user.ini檔案