jdbc 批量操作数据库(插入、更新)
2016-07-04 15:39
176 查看
更新一个的操作:
批量插入的操作:
批量更新的操作:
package jdbc.json; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import net.sf.json.JSONObject; import sun.dc.pr.PRError; public class Json { static String json_doctor="{\"alice\":{\"doctor_id\":1001,\"doctor_pwd\":\"111\"},\"tom\":{\"doctor_id\":1002,\"doctor_pwd\":\"111\"},\"cndy\":{\"doctor_id\":1003,\"doctor_pwd\":\"111\"}}"; public static void main(String[] args) { JSONdoctor(); JDBCc(); } public static JSONObject JSONdoctor(){ // System.out.println("json字符串--------------------"); // System.out.println(json_doctor); //将json字符串转为json对象。 JSONObject msg = JSONObject.fromObject(json_doctor); // System.out.println("json对象:"+msg); // System.out.println("json对象中的alice对象:"+msg.getJSONObject("alice")); // System.out.println("json对象中的tom对象:"+msg.getJSONObject("tom")); // System.out.println("json对象中的cndy对象的doctor_id属性:"+msg.getJSONObject("cndy").get("doctor_id")); // System.out.println(); //返回json对象 return msg; } public static void JDBCc(){ String url = "jdbc:mysql://172.16.1.72:3306/my_hospital"; String user = "root"; String pwd = "haoyi123"; Connection con = null; PreparedStatement pst = null; String sql = "update doctor_info set doctor_pwd =? where doctor_id = ?"; try { con = DriverManager.getConnection(url,user,pwd); pst = con.prepareStatement(sql); JSONObject json = JSONdoctor(); // System.out.println(json.getJSONObject("cndy").get("doctor_pwd")); // System.out.println(json.getJSONObject("cndy").get("doctor_id")); pst.setObject(1, json.getJSONObject("cndy").get("doctor_pwd")); pst.setObject(2, json.getJSONObject("cndy").get("doctor_id")); //执行SQL pst.executeUpdate(); } catch (Exception e) { // exception e.printStackTrace(); throw new RuntimeException("代购查询失败!"); }finally{ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
批量插入的操作:
package jdbc.json; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import net.sf.json.JSONObject; import sun.dc.pr.PRError; public class JSONinsert { static String json_doctor="{\"alice\":{\"doctor_id\":2101,\"doctor_pwd\":\"111\"},\"tom\":{\"doctor_id\":2102,\"doctor_pwd\":\"111\"},\"cndy\":{\"doctor_id\":3103,\"doctor_pwd\":\"111\"}}"; public static void main(String[] args) { JSONdoctor(); JDBCc(); } public static JSONObject JSONdoctor(){ // System.out.println("json字符串--------------------"); // System.out.println(json_doctor); //将json字符串转为json对象。 JSONObject msg = JSONObject.fromObject(json_doctor); // System.out.println("json对象:"+msg); // System.out.println("json对象中的alice对象:"+msg.getJSONObject("alice")); // System.out.println("json对象中的tom对象:"+msg.getJSONObject("tom")); // System.out.println("json对象中的cndy对象的doctor_id属性:"+msg.getJSONObject("cndy").get("doctor_id")); // System.out.println(); //返回json对象 return msg; } public static void JDBCc(){ String url = "jdbc:mysql://172.16.1.72:3306/my_hospital"; String user = "root"; String pwd = "haoyi123"; Connection con = null; PreparedStatement pst = null; String sql = "insert into doctor_info (doctor_id,doctor_pwd) values(?,?)"; try { con = DriverManager.getConnection(url,user,pwd); pst = con.prepareStatement(sql); JSONObject json = JSONdoctor(); // System.out.println(json.getJSONObject("cndy").get("doctor_pwd")); // System.out.println(json.getJSONObject("cndy").get("doctor_id")); pst.setObject(1, json.getJSONObject("cndy").get("doctor_id")); pst.setObject(2, json.getJSONObject("cndy").get("doctor_pwd")); pst.addBatch(); pst.setObject(1, json.getJSONObject("alice").get("doctor_id")); pst.setObject(2, json.getJSONObject("alice").get("doctor_pwd")); pst.addBatch(); pst.setObject(1, json.getJSONObject("tom").get("doctor_id")); pst.setObject(2, json.getJSONObject("tom").get("doctor_pwd")); pst.addBatch(); //执行SQL // pst.executeUpdate(); pst.executeBatch(); } catch (Exception e) { // exception e.printStackTrace(); throw new RuntimeException("代购查询失败!"); }finally{ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
批量更新的操作:
package jdbc.json; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import net.sf.json.JSONObject; import sun.dc.pr.PRError; public class JSONupdate { static String json_doctor="{\"alice\":{\"doctor_id\":1101,\"doctor_pwd\":\"222\"},\"tom\":{\"doctor_id\":1102,\"doctor_pwd\":\"222\"},\"cndy\":{\"doctor_id\":1103,\"doctor_pwd\":\"222\"}}"; public static void main(String[] args) { JSONdoctor(); JDBCc(); } public static JSONObject JSONdoctor(){ // System.out.println("json字符串--------------------"); // System.out.println(json_doctor); //将json字符串转为json对象。 JSONObject msg = JSONObject.fromObject(json_doctor); // System.out.println("json对象:"+msg); // System.out.println("json对象中的alice对象:"+msg.getJSONObject("alice")); // System.out.println("json对象中的tom对象:"+msg.getJSONObject("tom")); // System.out.println("json对象中的cndy对象的doctor_id属性:"+msg.getJSONObject("cndy").get("doctor_id")); // System.out.println(); //返回json对象 return msg; } public static void JDBCc(){ String url = "jdbc:mysql://172.16.1.72:3306/my_hospital"; String user = "root"; String pwd = "haoyi123"; Connection con = null; PreparedStatement pst = null; String sql = "update doctor_info set doctor_pwd =? where doctor_id = ?"; try { con = DriverManager.getConnection(url,user,pwd); pst = con.prepareStatement(sql); JSONObject json = JSONdoctor(); // System.out.println(json.getJSONObject("cndy").get("doctor_pwd")); // System.out.println(json.getJSONObject("cndy").get("doctor_id")); pst.setObject(1, json.getJSONObject("cndy").get("doctor_pwd")); pst.setObject(2, json.getJSONObject("cndy").get("doctor_id")); pst.addBatch(); pst.setObject(1, json.getJSONObject("alice").get("doctor_pwd")); pst.setObject(2, json.getJSONObject("alice").get("doctor_id")); pst.addBatch(); pst.setObject(1, json.getJSONObject("tom").get("doctor_pwd")); pst.setObject(2, json.getJSONObject("tom").get("doctor_id")); pst.addBatch(); //执行SQL // pst.executeUpdate(); pst.executeBatch(); //返回数组 } catch (Exception e) { // exception e.printStackTrace(); throw new RuntimeException("代购查询失败!"); }finally{ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
相关文章推荐
- Mysql学习笔记
- MySQL for Windows 解压缩版配置安装
- ORACLE客户端安装--ODP.NET用于VS2013
- 数据库设计
- MariaDB 数据库操作大全
- 关于今天mysql数据库的一系列问题
- Linux下安装Oracle11g
- MySQL常用命令
- 序列化XML的类。包括向SQL传XML数据
- SQL Server - 小鸟系列之全文搜索
- mysql多实例
- mysql拒绝访问(Error 1044/1045)问题的解决
- MySQL的源码安装及使用UDFs进行数据自动更新的教程
- plsql快速选中一行的快捷键
- hdwiki 数据库结构说明
- SQL设置SQLServer最大连接数
- Redis-sentinel哨兵模式集群方案配置
- 使用JDBC处理Oracle大数据
- windows mysql 自动备份的几种方法
- MySQL页以及每秒读写量评估的疑问