json mysql javabean_使用Flex,Java,Json更新Mysql数据

首先,创建一个数据库:在mysql提示框中输入以下的SQL就可以创建一个简单的员工信息资料表。

CREATE DATABASE IF NOT EXISTS test;

USE test;

DROP TABLE IF EXISTS `employee`;

CREATE TABLE `employee` (

`id` varchar(10) NOT NULL,

`name` varchar(45) NOT NULL,

`gender` varchar(10) NOT NULL,

`department` varchar(45) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

我们来先看看那后台java bean的处理:他要接受flex app的读取数据和更新数据的请求,而且他们之间的数据传递格式采用的是json.所以我们的java bean的一个框架结构应该是:

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 = "12345";

public Connection conToDB(){

try{

Class.forName(myDriver);

con = DriverManager.getConnection(conURL,userName,userPass);

}catch(Exception e){

e.printStackTrace();

}

return con;

}

public String getJsonArray(){

String result= new String();

return result;

}

public String sendJsonArray(String jsonData){

String result= new String();

return result;

}

}

里面包含了两个重要的方法(getJsonArray()和sendJsonArray())分别对应flex

app的读取数据和更新数据的请求。在getJsonArray()方法中,要连接数据库,取得员工的信息资料,然后按照json格式封装数据,结果返回

给flex app,由flex app中的datagrid显示出来。我们具体看看getJsonArray()这个方法:

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;

}

内容其实都很简单,只是读取数据和封装成json格式的数据,最后把json

array格式的jsonEmployeeArray转换成string格式传输给flex app.即return语句。而当flex

app要使用这个json

array格式的数据,自然需要按照json格式解码等,后面在介绍。接着看看那个更新数据的方法sendJsonArray():

public String sendJsonArray(String jsonData){

String result= new String();

//jsonData = jsonData.replace("\\", "");

JSONArray jsonArray = JSONArray.fromObject(jsonData);

try{

Connection conToDb = conToDB();

Statement stmt = conToDb.createStatement();

for(int i=0;i

JSONObject jsonObject = JSONObject.fromObject(jsonArray.getString(i));

String id = jsonObject.getString("id");

String name = jsonObject.getString("name");

stmt.executeUpdate("update employee set name='"+name+"' where id='"+id+"'");

}

result="恭喜,成功更新数据!";

conToDb.close();

}catch(Exception e){

e.printStackTrace();

}

return result;

}

即把flex

app传递过来的String类型的json格式的的数据解码开来,然后根据对应的Id把更新后的名字保存在数据库中。这里我们传递过来的是整个

datagrid的信息,不管是有没有更新的,都要循环的更新所有员工的信息。所以呢,在你的程序中你的JsonGrid.java文件应该类似:

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);

try{

Connection conToDb = conToDB();

Statement stmt = conToDb.createStatement();

for(int i=0;i

JSONObject jsonObject = JSONObject.fromObject(jsonArray.getString(i));

String id = jsonObject.getString("id");

String name = jsonObject.getString("name");

stmt.executeUpdate("update employee set name='"+name+"' where id='"+id+"'");

}

result="恭喜,成功更新数据!";

conToDb.close();

}catch(Exception e){

e.printStackTrace();

}

return result;

}

}

接下来我们看看flex app前台的处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值