java开发自建jdbc链接数据库执行存储过程
package com.peraglobal.rdms.project.utils;
import com.peraglobal.pdp.common.utils.StringUtil;
import com.peraglobal.pdp.core.utils.AppConfigUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import java.sql.*;
public class DatasourceManagerUtils implements InitializingBean {
Logger logger = LoggerFactory.getLogger(this.getClass());
private String url = "";
private String drivername = "";
private String userName = "";
private String password = "";
private Connection con = null;
private Statement stm = null;
private CallableStatement proc = null;
private static DatasourceManagerUtils datasourceManagerUtils = null ;
public static DatasourceManagerUtils getInstance() throws Exception{
if(datasourceManagerUtils==null){
String db_url = AppConfigUtils.get("db.url.master.default");
String db_uname = AppConfigUtils.get("jdbc.masterName");
String db_pwd = AppConfigUtils.get("jdbc.masterPasswd");
datasourceManagerUtils = new DatasourceManagerUtils(db_url, db_uname, db_pwd);
}
return datasourceManagerUtils;
}
@Override
public void afterPropertiesSet() throws Exception {
getInstance();
}
private DatasourceManagerUtils(String db_url, String db_uname, String db_pwd) throws Exception {
try {
this.url = db_url;
this.drivername = "oracle.jdbc.OracleDriver";
this.userName = db_uname;
this.password = db_pwd;
Class.forName(this.drivername).newInstance();
} catch (Exception e) {
e.printStackTrace();
throw new Exception("数据库连接异常");
}
}
public Connection getDBCon() throws Exception {
if (StringUtil.isNotEmpty(this.url)) {
try {
System.out.println(this.url);
DriverManager.setLoginTimeout(30);
this.con = DriverManager.getConnection(this.url, this.userName, this.password);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("ERP数据库连接异常");
}
} else {
System.out.println("未设置数据库参数");
throw new Exception("未设置数据库参数");
}
return this.con;
}
public void createCon() throws Exception {
this.con = DriverManager.getConnection(this.url, this.userName, this.password);
}
public void getStm() throws Exception {
createCon();
this.stm = this.con.createStatement();
}
public int executeUpdate(String sql) throws Exception {
int mark = 0;
getStm();
int iCount = this.stm.executeUpdate(sql);
if (iCount > 0)
mark = 1;
else
mark = 0;
return mark;
}
public ResultSet executeQuery(String sql) throws Exception {
ResultSet rs = null;
getStm();
try {
rs = this.stm.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
logger.error("查询数据库失败!");
}
return rs;
}
public boolean execute(String sql) throws Exception {
boolean flag = false;
getStm();
try {
flag = this.stm.execute(sql);
} catch (Exception e) {
e.printStackTrace();
logger.error("sql执行失败!");
}
return flag;
}
public int executeProlifecycletoasstaskitem(String lifeCycleRootId,String assTaskRootId,String versionId,String projectId) throws Exception {
int k = 0;
try {
createCon();
this.proc = this.con.prepareCall("{CALL tips_package.pro_lifecycletoasstaskitem(?,?,?,?,?)}");
this.proc.setString(1, lifeCycleRootId);
this.proc.setString(2, assTaskRootId);
this.proc.setString(3, assTaskRootId);
this.proc.setString(4, versionId);
this.proc.setString(5, projectId);
k = this.proc.executeUpdate();
this.con.commit();
} catch (SQLException e) {
e.printStackTrace();
throw new Exception(e.getMessage());
} finally {
this.closed();
}
return k;
}
public void closed() {
if (this.stm != null)
try {
this.stm.close();
} catch (SQLException e) {
e.printStackTrace();
logger.error("关闭stm对象失败!");
}
if (this.proc != null)
try {
this.proc.close();
} catch (SQLException e) {
e.printStackTrace();
logger.error("关闭proc对象失败!");
}
if (this.con != null)
try {
this.con.close();
} catch (SQLException e) {
e.printStackTrace();
logger.error("关闭con对象失败!");
}
}
}
分享到:
相关推荐
这是一本以面试题为入口讲解 Java 核心内容的技术书籍,书中内容极力的向你证实代码是对数学逻辑的具体实现。当你仔细阅读书籍时,会发现Java中有大量的数学知识,包括:扰动函数、负载因子、拉链寻址、开放寻址、...
适用于期末数据库课设 机票预订系统带项目说明书 java+mysql 完美运行 2022
effective-java 自己用网上的翻译的html版本,生成的pdf版,仅供参考,仅供参考,仅供参考,仅供参考
java_面试指北PDF版本(最新)
从javacv-platform-1.3.3-bin.zip中抽出来的:javacpp.jar、javacv.jar、javacv-platform.jar、opencv.jar、opencv-android-arm.jar、opencv-android-x86.jar、opencv-linux-armhf.jar 、opencv-linux-ppc64le.jar、...
java简历模板,适合开发者找工作参考
windows版本的JAVA离线安装包!!!不适用windows xp,慎重下载~!!!离线可以安装离线!!离线可以安装!
Java基础 java学习笔记 Java Java基础Markdown学习笔记,可转换成PDF、Word等格式
自己总结的java webService接口开发案例,希望能够帮助我需要的朋友。
Java互联网实时聊天系统源码.zip