您的位置:首页 > 数据库

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

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: