json数据持久到MySQL

create table jsontest(
phone varchar(255),
name  varchar(255),
age int(8)
)

test.json

{'profiles': [
  {'name':'John', 'age': 44, 'phone':'203-203-2030'}, 
  {'name':'Alex','age':31, 'phone':'203-203-2030'},
  {'name':'Amy', 'age': 24, 'phone':'203-203-2030'}, 
  {'name':'Melissa','age':21, 'phone':'203-203-2030'}
 ]
}
package liulixin;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
 
import org.apache.commons.io.IOUtils;

public class MyJson {
	private static String tableName = "jsontest";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			   ClassLoader cl = MyJson.class.getClassLoader();
			   InputStream is = cl.getResourceAsStream("test.json");
			   String str = IOUtils.toString(is);
			   JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(str);
			   JSONArray jsonArr = jsonObject.getJSONArray("profiles");
			 
			   JSONObject obj = null;
			   JSONArray nameArr = null;
			   JSONArray valArr = null;
			    
			   for (int i = 0; i < jsonArr.size(); i++) {
			    obj = jsonArr.getJSONObject(i);
			    nameArr = obj.names();
			    System.out.println(nameArr);
			    valArr = obj.toJSONArray(nameArr);
			    saveRecord(nameArr, valArr);
			    System.out.println(valArr);
			   }
			 
			  } catch (Exception e) {
			   e.printStackTrace();
			  }
		
		//test con
		getConnection();

	}
	
	private static Connection getConnection() {
		 
		  Connection con = null;
		  String url = "jdbc:mysql://localhost:3306/";
		  String db =  "puser";
		  String driver = "com.mysql.jdbc.Driver";
		  String user = "root";
		  String pass = "";
		 
		  try {
		   Class.forName(driver);
		   con = DriverManager.getConnection(url + db, user, pass);
		  } catch (ClassNotFoundException e) {
		   e.printStackTrace();
		  } catch (SQLException e) {
		   e.printStackTrace();
		  }
		  
		  System.out.println("fucked up");
		 
		  return con;
		 }
	
	 private static void bindVariables(JSONArray valArray,
			   PreparedStatement pstmt) throws SQLException {
			 
			  Iterator<Object> iterator = valArray.iterator();
			  int cnt = 0;
			  while (iterator.hasNext()) {
			 
			   Object obj = iterator.next();
			   if (obj instanceof String) {
			    pstmt.setString(++cnt, (String) obj);
			   } else if (obj instanceof Integer) {
			    pstmt.setLong(++cnt, (Integer) obj);
			   } else if (obj instanceof Long) {
			    pstmt.setLong(++cnt, (Long) obj);
			   } else if (obj instanceof Double) {
			    pstmt.setDouble(++cnt, (Double) obj);
			   }
			  }
			  }
	 
	 private static void saveRecord(JSONArray nameArray, JSONArray valArray) {
		 
		  Connection conn = getConnection();
		  StringBuffer sb = new StringBuffer("insert into " + tableName + "(");
		 
		  int size = nameArray.size();
		  int count = 0;
		  Iterator<Object> iterator = nameArray.iterator();
		   
		  while (iterator.hasNext()) {
		   if (count < (size - 1))
		    sb.append(iterator.next() + ",");
		   else
		    sb.append(iterator.next() + ")");
		 
		   count++;
		  }
		 
		  sb.append(" values(");
		  
		  for (int i = 0; i < size; i++) {   
		   if (i < (size - 1))
		    sb.append("?,");
		   else
		    sb.append("?)");   
		  }
		 
		  System.out.println(sb.toString());
		 
		  try {
		   PreparedStatement pstmt = conn.prepareStatement(sb.toString());
		   bindVariables(valArray, pstmt);
		   pstmt.executeUpdate();
		 
		  } catch (SQLException e) {
		   e.printStackTrace();
		  }
		 }
		 
		

}

源代码:http://pan.baidu.com/share/link?shareid=2763213529&uk=3878681452

原文:http://asmitasapkota.blogspot.com/2012/01/here-is-simple-example-where-we-are.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值