}
279
/*
280
* 调用存储过程,并有输出参数
281
* @procName ,存储过程名称:proc_Insert(?,?)
282
* @in ,输入参数集合
283
* @output,输出参数集合
284
* @type,输出参数类型集合
285
*
*/
286
public
static
Object executeOutputProcedure(String procName,
287
Object[] in,Object[] output,
int
[] type){
288
Connection conn =
getConnection();
289
Object result =
null
;
290
try
{
291
CallableStatement cstmt = conn.prepareCall("{call "+procName+"}"
);
292
//
设置存储过程的参数值
293
int
i=0
;
294
for
(;i<in.length;i++){
//
设置输入参数
295
cstmt.setObject(i+1
, in[i]);
296
//
print(i+1);
297
}
298
int
len = output.length+
i;
299
for
(;i<len;i++){
//
设置输出参数
300
cstmt.registerOutParameter(i+1,type[i-
in.length]);
301
//
print(i+1);
302
}
303
boolean
b =
cstmt.execute();
304
//
获取输出参数的值
305
for
(i=in.length;i<output.length+in.length;i++
)
306
output[i-in.length] = cstmt.getObject(i+1
);
307
if
(b) {
308
result =
cstmt.getResultSet();
309
}
310
else
{
311
result =
cstmt.getUpdateCount();
312
}
313
}
catch
(SQLException e) {
314
//
TODO Auto-generated catch block
315
e.printStackTrace();
316
}
317
return
result;
318
}
319
public
static
String toJson(Object obj){
320
String reuqest=
null
;
321
//
对象映射
322
ObjectMapper mapper=
new
ObjectMapper();
323
//
设置时间格式
324
SimpleDateFormat dateFormat=
new
SimpleDateFormat("yyyy年MM月dd日"
);
325
mapper.setDateFormat(dateFormat);
326
try
{
327
reuqest=
mapper.writeValueAsString(obj);
328
}
catch
(JsonProcessingException e) {
329
//
TODO Auto-generated catch block
330
e.printStackTrace();
331
}
332
return
reuqest;
333
}
334
public
static
<T> T toObject(String src,Class<T>
valueType){
335
T request=
null
;
336
//
对象反射
337
ObjectMapper mapper=
new
ObjectMapper();
338
try
{
339
request=
mapper.readValue(src, valueType);
340
}
catch
(JsonParseException e) {
341
//
TODO Auto-generated catch block
342
e.printStackTrace();
343
}
catch
(JsonMappingException e) {
344
//
TODO Auto-generated catch block
345
e.printStackTrace();
346
}
catch
(IOException e) {
347
//
TODO Auto-generated catch block
348
e.printStackTrace();
349
}
350
return
request;
351
}
352
public
static
Date date(String date_str) {
353
try
{
354
Calendar zcal = Calendar.getInstance();
//
日期类
355
Timestamp timestampnow =
new
Timestamp(zcal.getTimeInMillis());
//
转换成正常的日期格式
356
SimpleDateFormat formatter =
new
SimpleDateFormat("yyyy-MM-dd");
//
改为需要的东西
357
ParsePosition pos =
new
ParsePosition(0
);
358
java.util.Date current =
formatter.parse(date_str, pos);
359
timestampnow =
new
Timestamp(current.getTime());
360
return
timestampnow;
361
}
362
catch
(NullPointerException e) {
363
return
null
;
364
}
365
}
366
}
声明:使用该公共方法需要在WEB-INF/classes/jdbc.properties添加连接驱动等等 如图:
在classes中加入链接
1 DBDRIVER=com.microsoft.sqlserver.jdbc.SQLServerDriver
2 DBURL=jdbc:sqlserver://localhost:1433;databasename=AJAX_ProductDB
3 DBUID=sa
4 DBPWD=123456