轮询程序中使用的各种方法:spring调用;http调用;存储过程调用;
2016-12-12 15:18
633 查看
[java]
view plain
copy
print?
/**
package com.wonders.schedule.util;
import java.io.BufferedReader;
/**
* @ClassName: ExecUtil
* @Description: TODO(这里用一句话描述这个类的作用)
* @author zhoushun
* @date 2012-12-5 下午02:49:26
*
*/
public class ExecUtil {
/**
* @Title: nativeExec
* @Description: TODO(任务调用 本地)
* @param @param t
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String nativeExec(TScheduleConfig t) {
String method = t.getMethod();
String param = t.getParam();
String result = "";
if (method != null && method.indexOf(".") > -1) {
String className = (method.split("\\."))[0];
String methodName = (method.split("\\."))[1];
try {
Thread.sleep(2000);
ITaskService task = (ITaskService) SpringBeanUtil
.getBean(className);
Class<?> cls = task.getClass();
Method[] methods = cls.getDeclaredMethods();
String methodsName = "";
for (Method m : methods) {
methodsName += m.getName() + ",";
}
if (methodsName.indexOf(methodName) >= 0) {
// 返回方法名为“testMethod”的一个 Method 对象,后面跟的是该方法参数
Method callMethod = cls.getMethod(methodName,
new Class[] { String.class });
result = (String) callMethod.invoke(task,
new Object[] { param });
}
} catch (Exception e) {
e.printStackTrace();
result = "0";
}
}
return result;
}
/**
* @Title: webExec
* @Description: TODO(任务调用 网络)
* @param @param t
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String webExec(TScheduleConfig t) {
String method = t.getMethod();
String param = t.getParam();
String result = "";
try {
URL url = null;
HttpURLConnection http = null;
try {
Thread.sleep(2000);
url = new URL(method);
http = (HttpURLConnection) url.openConnection();
http.setDoInput(true);
http.setDoOutput(true);
http.setUseCaches(false);
http.setConnectTimeout(50000);
http.setReadTimeout(50000);
http.setRequestMethod("POST");
// http.setRequestProperty("Content-Type",
// "text/xml; charset=UTF-8");
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.connect();
param = "¶m=" + param;
OutputStreamWriter osw = new OutputStreamWriter(http
.getOutputStream(), "utf-8");
osw.write(param);
osw.flush();
osw.close();
if (http.getResponseCode() == 200) {
BufferedReader in = new BufferedReader(
new InputStreamReader(http.getInputStream(),
"utf-8"));
String inputLine;
while ((inputLine = in.readLine()) != null) {
result += inputLine;
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
result = "0";
} finally {
if (http != null)
http.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 返回值为String
/**
* @Title: procedureExec
* @Description: TODO(任务调用 数据库)
* @param @param t
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
@SuppressWarnings("unchecked")
public static String procedureExec(TScheduleConfig t) {
final String datasource = t.getDatasource();
final String method = t.getMethod();
final String param = t.getParam();
JdbcTemplate jdbcTemplate = DbUtil.getJdbcTemplate(datasource);
String result = "";
try {
Thread.sleep(2000);
result = (String) jdbcTemplate.execute(
new CallableStatementCreator() {
public CallableStatement createCallableStatement(
Connection con) throws SQLException {
String storedProc = "{call " + method + "(?,?)}";// 调用的sql
CallableStatement cs = con.prepareCall(storedProc);
cs.setString(1, param);// 设置输入参数的值
cs.registerOutParameter(2, OracleTypes.VARCHAR);// 注册输出参数的类型
return cs;
}
}, new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs)
throws SQLException, DataAccessException {
cs.execute();
return cs.getString(2);// 获取输出参数的值
}
});
} catch (Exception e) {
e.printStackTrace();
result = "0";
}
return result;
}
// 返回值为游标 遍历后的List<Map>
/**
* @Title: procedureExec2
* @Description: TODO(任务调用数据库)
* @param @param t
* @param @return 游标类型 转换为 list<Map>结构
* @return List<Map<String,Object>> 返回类型
* @throws
*/
@SuppressWarnings("unchecked")
public static List<Map<String, Object>> procedureExec2(TScheduleConfig t) {
String datasource = "";
final String method = t.getMethod();
final String param = t.getParam();
JdbcTemplate jdbcTemplate = DbUtil.getJdbcTemplate(datasource);
List resultList = null;
try {
resultList = (List) jdbcTemplate.execute(
new CallableStatementCreator() {
public CallableStatement createCallableStatement(
Connection con) throws SQLException {
String storedProc = "{call " + method + "(?,?)}";// 调用的sql
CallableStatement cs = con.prepareCall(storedProc);
cs.setString(1, param);// 设置输入参数的值
cs.registerOutParameter(2, OracleTypes.CURSOR);// 注册输出参数的类型
return cs;
}
}, new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs)
throws SQLException, DataAccessException {
List resultsMap = new ArrayList();
cs.execute();
ResultSet rs = (ResultSet) cs.getObject(2);// 获取游标一行的值
while (rs.next()) {// 转换每行的返回值到Map中
Map rowMap = new HashMap();
rowMap.put("param1", rs.getString("param1"));
rowMap.put("param2", rs.getString("param2"));
resultsMap.add(rowMap);
}
rs.close();
return resultsMap;
}
});
} catch (Exception e) {
e.printStackTrace();
}
/*
* for (int i = 0; i < resultList.size(); i++) { Map rowMap = (Map)
* resultList.get(i); String id = rowMap.get("id").toString(); String
* name = rowMap.get("name").toString(); System.out.println("id=" + id +
* ";name=" + name); }
*/
return resultList;
}
}
view plain
copy
print?
/**
package com.wonders.schedule.util;
import java.io.BufferedReader;
/**
* @ClassName: ExecUtil
* @Description: TODO(这里用一句话描述这个类的作用)
* @author zhoushun
* @date 2012-12-5 下午02:49:26
*
*/
public class ExecUtil {
/**
* @Title: nativeExec
* @Description: TODO(任务调用 本地)
* @param @param t
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String nativeExec(TScheduleConfig t) {
String method = t.getMethod();
String param = t.getParam();
String result = "";
if (method != null && method.indexOf(".") > -1) {
String className = (method.split("\\."))[0];
String methodName = (method.split("\\."))[1];
try {
Thread.sleep(2000);
ITaskService task = (ITaskService) SpringBeanUtil
.getBean(className);
Class<?> cls = task.getClass();
Method[] methods = cls.getDeclaredMethods();
String methodsName = "";
for (Method m : methods) {
methodsName += m.getName() + ",";
}
if (methodsName.indexOf(methodName) >= 0) {
// 返回方法名为“testMethod”的一个 Method 对象,后面跟的是该方法参数
Method callMethod = cls.getMethod(methodName,
new Class[] { String.class });
result = (String) callMethod.invoke(task,
new Object[] { param });
}
} catch (Exception e) {
e.printStackTrace();
result = "0";
}
}
return result;
}
/**
* @Title: webExec
* @Description: TODO(任务调用 网络)
* @param @param t
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String webExec(TScheduleConfig t) {
String method = t.getMethod();
String param = t.getParam();
String result = "";
try {
URL url = null;
HttpURLConnection http = null;
try {
Thread.sleep(2000);
url = new URL(method);
http = (HttpURLConnection) url.openConnection();
http.setDoInput(true);
http.setDoOutput(true);
http.setUseCaches(false);
http.setConnectTimeout(50000);
http.setReadTimeout(50000);
http.setRequestMethod("POST");
// http.setRequestProperty("Content-Type",
// "text/xml; charset=UTF-8");
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.connect();
param = "¶m=" + param;
OutputStreamWriter osw = new OutputStreamWriter(http
.getOutputStream(), "utf-8");
osw.write(param);
osw.flush();
osw.close();
if (http.getResponseCode() == 200) {
BufferedReader in = new BufferedReader(
new InputStreamReader(http.getInputStream(),
"utf-8"));
String inputLine;
while ((inputLine = in.readLine()) != null) {
result += inputLine;
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
result = "0";
} finally {
if (http != null)
http.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 返回值为String
/**
* @Title: procedureExec
* @Description: TODO(任务调用 数据库)
* @param @param t
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
@SuppressWarnings("unchecked")
public static String procedureExec(TScheduleConfig t) {
final String datasource = t.getDatasource();
final String method = t.getMethod();
final String param = t.getParam();
JdbcTemplate jdbcTemplate = DbUtil.getJdbcTemplate(datasource);
String result = "";
try {
Thread.sleep(2000);
result = (String) jdbcTemplate.execute(
new CallableStatementCreator() {
public CallableStatement createCallableStatement(
Connection con) throws SQLException {
String storedProc = "{call " + method + "(?,?)}";// 调用的sql
CallableStatement cs = con.prepareCall(storedProc);
cs.setString(1, param);// 设置输入参数的值
cs.registerOutParameter(2, OracleTypes.VARCHAR);// 注册输出参数的类型
return cs;
}
}, new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs)
throws SQLException, DataAccessException {
cs.execute();
return cs.getString(2);// 获取输出参数的值
}
});
} catch (Exception e) {
e.printStackTrace();
result = "0";
}
return result;
}
// 返回值为游标 遍历后的List<Map>
/**
* @Title: procedureExec2
* @Description: TODO(任务调用数据库)
* @param @param t
* @param @return 游标类型 转换为 list<Map>结构
* @return List<Map<String,Object>> 返回类型
* @throws
*/
@SuppressWarnings("unchecked")
public static List<Map<String, Object>> procedureExec2(TScheduleConfig t) {
String datasource = "";
final String method = t.getMethod();
final String param = t.getParam();
JdbcTemplate jdbcTemplate = DbUtil.getJdbcTemplate(datasource);
List resultList = null;
try {
resultList = (List) jdbcTemplate.execute(
new CallableStatementCreator() {
public CallableStatement createCallableStatement(
Connection con) throws SQLException {
String storedProc = "{call " + method + "(?,?)}";// 调用的sql
CallableStatement cs = con.prepareCall(storedProc);
cs.setString(1, param);// 设置输入参数的值
cs.registerOutParameter(2, OracleTypes.CURSOR);// 注册输出参数的类型
return cs;
}
}, new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs)
throws SQLException, DataAccessException {
List resultsMap = new ArrayList();
cs.execute();
ResultSet rs = (ResultSet) cs.getObject(2);// 获取游标一行的值
while (rs.next()) {// 转换每行的返回值到Map中
Map rowMap = new HashMap();
rowMap.put("param1", rs.getString("param1"));
rowMap.put("param2", rs.getString("param2"));
resultsMap.add(rowMap);
}
rs.close();
return resultsMap;
}
});
} catch (Exception e) {
e.printStackTrace();
}
/*
* for (int i = 0; i < resultList.size(); i++) { Map rowMap = (Map)
* resultList.get(i); String id = rowMap.get("id").toString(); String
* name = rowMap.get("name").toString(); System.out.println("id=" + id +
* ";name=" + name); }
*/
return resultList;
}
}
/** package com.wonders.schedule.util; import java.io.BufferedReader; /** * @ClassName: ExecUtil * @Description: TODO(这里用一句话描述这个类的作用) * @author zhoushun * @date 2012-12-5 下午02:49:26 * */ public class ExecUtil { /** * @Title: nativeExec * @Description: TODO(任务调用 本地) * @param @param t * @param @return 设定文件 * @return String 返回类型 * @throws */ public static String nativeExec(TScheduleConfig t) { String method = t.getMethod(); String param = t.getParam(); String result = ""; if (method != null && method.indexOf(".") > -1) { String className = (method.split("\\."))[0]; String methodName = (method.split("\\."))[1]; try { Thread.sleep(2000); ITaskService task = (ITaskService) SpringBeanUtil .getBean(className); Class<?> cls = task.getClass(); Method[] methods = cls.getDeclaredMethods(); String methodsName = ""; for (Method m : methods) { methodsName += m.getName() + ","; } if (methodsName.indexOf(methodName) >= 0) { // 返回方法名为“testMethod”的一个 Method 对象,后面跟的是该方法参数 Method callMethod = cls.getMethod(methodName, new Class[] { String.class }); result = (String) callMethod.invoke(task, new Object[] { param }); } } catch (Exception e) { e.printStackTrace(); result = "0"; } } return result; } /** * @Title: webExec * @Description: TODO(任务调用 网络) * @param @param t * @param @return 设定文件 * @return String 返回类型 * @throws */ public static String webExec(TScheduleConfig t) { String method = t.getMethod(); String param = t.getParam(); String result = ""; try { URL url = null; HttpURLConnection http = null; try { Thread.sleep(2000); url = new URL(method); http = (HttpURLConnection) url.openConnection(); http.setDoInput(true); http.setDoOutput(true); http.setUseCaches(false); http.setConnectTimeout(50000); http.setReadTimeout(50000); http.setRequestMethod("POST"); // http.setRequestProperty("Content-Type", // "text/xml; charset=UTF-8"); http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); http.connect(); param = "¶m=" + param; OutputStreamWriter osw = new OutputStreamWriter(http .getOutputStream(), "utf-8"); osw.write(param); osw.flush(); osw.close(); if (http.getResponseCode() == 200) { BufferedReader in = new BufferedReader( new InputStreamReader(http.getInputStream(), "utf-8")); String inputLine; while ((inputLine = in.readLine()) != null) { result += inputLine; } in.close(); } } catch (Exception e) { e.printStackTrace(); result = "0"; } finally { if (http != null) http.disconnect(); } } catch (Exception e) { e.printStackTrace(); } return result; } // 返回值为String /** * @Title: procedureExec * @Description: TODO(任务调用 数据库) * @param @param t * @param @return 设定文件 * @return String 返回类型 * @throws */ @SuppressWarnings("unchecked") public static String procedureExec(TScheduleConfig t) { final String datasource = t.getDatasource(); final String method = t.getMethod(); final String param = t.getParam(); JdbcTemplate jdbcTemplate = DbUtil.getJdbcTemplate(datasource); String result = ""; try { Thread.sleep(2000); result = (String) jdbcTemplate.execute( new CallableStatementCreator() { public CallableStatement createCallableStatement( Connection con) throws SQLException { String storedProc = "{call " + method + "(?,?)}";// 调用的sql CallableStatement cs = con.prepareCall(storedProc); cs.setString(1, param);// 设置输入参数的值 cs.registerOutParameter(2, OracleTypes.VARCHAR);// 注册输出参数的类型 return cs; } }, new CallableStatementCallback() { public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException { cs.execute(); return cs.getString(2);// 获取输出参数的值 } }); } catch (Exception e) { e.printStackTrace(); result = "0"; } return result; } // 返回值为游标 遍历后的List<Map> /** * @Title: procedureExec2 * @Description: TODO(任务调用数据库) * @param @param t * @param @return 游标类型 转换为 list<Map>结构 * @return List<Map<String,Object>> 返回类型 * @throws */ @SuppressWarnings("unchecked") public static List<Map<String, Object>> procedureExec2(TScheduleConfig t) { String datasource = ""; final String method = t.getMethod(); final String param = t.getParam(); JdbcTemplate jdbcTemplate = DbUtil.getJdbcTemplate(datasource); List resultList = null; try { resultList = (List) jdbcTemplate.execute( new CallableStatementCreator() { public CallableStatement createCallableStatement( Connection con) throws SQLException { String storedProc = "{call " + method + "(?,?)}";// 调用的sql CallableStatement cs = con.prepareCall(storedProc); cs.setString(1, param);// 设置输入参数的值 cs.registerOutParameter(2, OracleTypes.CURSOR);// 注册输出参数的类型 return cs; } }, new CallableStatementCallback() { public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException { List resultsMap = new ArrayList(); cs.execute(); ResultSet rs = (ResultSet) cs.getObject(2);// 获取游标一行的值 while (rs.next()) {// 转换每行的返回值到Map中 Map rowMap = new HashMap(); rowMap.put("param1", rs.getString("param1")); rowMap.put("param2", rs.getString("param2")); resultsMap.add(rowMap); } rs.close(); return resultsMap; } }); } catch (Exception e) { e.printStackTrace(); } /* * for (int i = 0; i < resultList.size(); i++) { Map rowMap = (Map) * resultList.get(i); String id = rowMap.get("id").toString(); String * name = rowMap.get("name").toString(); System.out.println("id=" + id + * ";name=" + name); } */ return resultList; } }
相关文章推荐
- 轮询程序中使用的各种方法:spring调用;http调用;存储过程调用;
- 轮询程序中使用的各种方法:spring调用;http调用;存储过程调用;
- (转载)使用DbHelperSQL调用存储过程的方法
- Spring中HttpInvoker远程方法调用使用实例
- C#中的类SqlCommand对象使用方法ExecuteNonQuery()调用SQLServer存储过程时,存储过程执行成功,数据发生改变,但是返回-1
- 使用Spring的JdbcTemplate调用Oracle的存储过程
- 调用存储过程使用aspnetpager,加载时无法设定默认为第二页的方法
- 如何调用 Oracle 存储过程在 Visual Basic.NET 使用 Microsoft Oracle 托管提供程序
- Spring如何使用JdbcTemplate调用存储过程的三种情况
- 使用Spring的HttpInovoker进行远程方法调用
- 使用Spring的JdbcTemplate调用Oracle的存储过程
- 如何调用 Oracle 存储过程在 Visual Basic.NET 使用 Microsoft Oracle 托管提供程序
- bboss persistent框架调用存储过程的返回结果为xml串的使用方法
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)
- java 程序加载过程---3--类中申明同时申明类的静态对象 创建类的实例 访问类的静态变量 调用类的静态方法 使用反射方法 初始化类的子类对象 直接使用java.exe 调用某个类
- [原]Spring如何使用JdbcTemplate调用存储过程的三种情况
- Spring如何使用JdbcTemplate调用存储过程的三种情况
- Spring如何使用JdbcTemplate调用存储过程的三种情况
- Spring如何使用JdbcTemplate调用存储过程的三种情况
- 关于使用 Hibernate 调用存储过程方法