package jsongrid;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonGrid {
private Connection con = null;
private String myDriver = "com.mysql.jdbc.Driver";
private String conURL = "jdbc:mysql://localhost:3306/test";
private String userName = "root";
private String userPass = "liceven";
public Connection conToDB(){
try{
Class.forName(myDriver);
con = DriverManager.getConnection(conURL,userName,userPass);
}catch(Exception e){
e.printStackTrace();
}
return con;
}
public String getJsonArray(){
JSONArray jsonEmployeeArray = new JSONArray();
ResultSet rs = null;
String result= new String();
try{
Connection conToDb = conToDB();
Statement stmt = conToDb.createStatement();
rs=stmt.executeQuery("select * from employee");
while(rs.next()){
JSONObject jsonEmployee = new JSONObject();
jsonEmployee.put("id", rs.getString("id"));
jsonEmployee.put("name", rs.getString("name"));
jsonEmployee.put("gender", rs.getString("gender"));
jsonEmployee.put("department", rs.getString("department"));
jsonEmployeeArray.add(jsonEmployee);
}
result = jsonEmployeeArray.toString();
conToDb.close();
//result = new JSONObject().put("jsonEmployeeArray",jsonEmployeeArray).toString();
}catch(SQLException ex){
ex.printStackTrace();
}
return result;
}
public String sendJsonArray(String jsonData){
String result= new String();
//jsonData = jsonData.replace("\\", "");
JSONArray jsonArray = JSONArray.fromObject(jsonData);
System.out.println(jsonArray.toString());
try{
Connection conToDb = conToDB();
Statement stmt = conToDb.createStatement();
for(int i=0;i<jsonArray.size();i++){
JSONObject jsonObject = JSONObject.fromObject(jsonArray.getString(i));
String operation = jsonObject.getString("operation");
if(operation.equals("update")){
String name = jsonObject.getString("name");
String id = jsonObject.getString("id");
stmt.executeUpdate("update employee set name='"+name+"' where id='"+id+"'");
result="恭喜,成功更新"+name+"数据!";
System.out.println(result);
}else if(operation.equals("delete")){
String id = jsonObject.getString("id");
stmt.executeUpdate("delete from employee where id = '"+id+"'");
result="恭喜,成功删除"+id+"数据!";
}
//String name = jsonObject.getString("name");
//stmt.executeUpdate("update employee set name='"+name+"' where id='"+id+"'");
}
conToDb.close();
}catch(Exception e){
result = e.getLocalizedMessage();
}
return result;
}
}
黑夜愁客
- 粉丝: 81
- 资源: 48
最新资源
- 保险箱检测51-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 五相电机邻近四矢量SVPWM模型-MATLAB-Simulink仿真模型包括: (1)原理说明文档(重要):包括扇区判断、矢量作用时间计算、矢量作用顺序及切时间计算、PWM波的生成; (2)输出部分仿
- 一对一MybatisProgram.zip
- 时变动态分位数CoVaR、delta-CoVaR,分位数回归 △CoVaR测度 溢出效应 动态 Adrian2016基于分位数回归方法计算动态条件在险价值 R语言代码,代码更数据就能用,需要修改的
- 人物检测37-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 人物检测26-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 人和箱子检测2-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 清华大学2022年秋季学期 高等数值分析课程报告
- GEE错误集-Cannot add an object of type <Element> to the map. Might be fixable with an explicit .pdf
- 清华大学2022年秋季学期 高等数值分析课程报告
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页