package com.datainfo.utils;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.datainfo.utils.log4j.LoggerMessage;
import com.mysql.jdbc.Connection;
public class BaseDAO {
private static final Logger log = Logger.getLogger(LoggerMessage.class);
private static String url = null;
private static String username = null;
private static String password = null;
private static String driver=null;
private static Properties props = new Properties();
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private static final BaseDAO baseDao = new BaseDAO();
private static final int String = 0;
private CallableStatement call;
private BaseDAO() {
}
//单例模式
public static BaseDAO getInstance() {
return baseDao;
}
//初始化加载
static {
try {
props.load(BaseDAO.class.getClassLoader().getResourceAsStream("jdbc.properties"));
} catch (IOException e) {
log.info("加载jdbc.properties配置文件异常,请检查!");
}
url =(props.getProperty("url"));
username = (props.getProperty("username"));
password = (props.getProperty("password"));
driver=(props.getProperty("driverClassName"));
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
log.info("加载数据库驱动异常,请检查!");
}
}
//打开数据库连接
public Connection getConnection() {
try {
Class.forName(driver);
conn = (Connection) DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
System.out.println("加载驱动出现异常!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭数据库连接
public void closeConnection(Connection conn) {
if (conn == null) return;
try {
if (!conn.isClosed()) {
//关闭数据库连接
conn.close();
}
} catch (SQLException e) {
log.info("关闭数据库连接发生异常,请检查!");
}
}
//查询
public ResultSet extuceQuery(String sql, Object[] values) {
conn = getConnection();
try {
pstmt = conn.prepareStatement(sql);
setValue(values);
rs = pstmt.executeQuery();
} catch (SQLException e) {
System.out.println("查询数据出现异常!");
e.printStackTrace();
}
return rs;
}
//添加 修改 删除
public boolean extuceUpdate(String sql, Object[] values) {
getConnection();
boolean b = false;
try {
//
pstmt = conn.prepareStatement(sql);
setValue(values);
int i = pstmt.executeUpdate();
if (i >= 1) {
b = true;
}
} catch (SQLException e) {
System.out.println("修改数据出现异常!");
e.printStackTrace();
}
return b;
}
//调用存储过程的方法(无返回值)
public String callProcedure(String sql,Object[] values,int index){//index为输出参数的位置
getConnection();
String flag = null;
try {
call= conn.prepareCall(sql);
setValues(values);
call.registerOutParameter(index, String);
call.executeUpdate();
flag=call.getString(index);
System.out.println(call.toString());
} catch (SQLException e) {
e.printStackTrace();
}finally{
close();
}
return flag;
}
//关闭所有
public void close() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (call != null) {
try {
call.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
if (conn != null && (!conn.isClosed())) {
conn.close();
}
} catch (SQLException e) {
}
}
}
//关闭所有
public void closeAll() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
if (conn != null && (!conn.isClosed())) {
conn.close();
}
} catch (SQLException e) {
}
}
}
//为占位符赋值
public void setValue(Object[] values) {
if (values != null && values.length != 0)
for (int i = 0; i < values.length; i++) {
try {
pstmt.setObject(i + 1, values[i]);
} catch (SQLException e) {
System.out.println("为占位符赋值出现异常!");
e.printStackTrace();
}
}
}
//为存储过程中的占位符赋值
public void setValues(Object[] values) {
if (values != null && values.length != 0)
for (int i = 0; i < values.length; i++) {
try {
call.setObject(i + 1, values[i]);
} catch (SQLException e) {
System.out.println("为存储过程的占位符赋值出现异常!");
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String sql="call userDepositActive2(?,?,?,?)";
Object values[]={1,3,200};
System.out.println(getInstance().callProcedure(sql, values,4));
// String sql1="call userDepositActive1(?)";
//
// System.out.println(getInstance().callProcedure(sql1, null,1));
}
}
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.datainfo.utils.log4j.LoggerMessage;
import com.mysql.jdbc.Connection;
public class BaseDAO {
private static final Logger log = Logger.getLogger(LoggerMessage.class);
private static String url = null;
private static String username = null;
private static String password = null;
private static String driver=null;
private static Properties props = new Properties();
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private static final BaseDAO baseDao = new BaseDAO();
private static final int String = 0;
private CallableStatement call;
private BaseDAO() {
}
//单例模式
public static BaseDAO getInstance() {
return baseDao;
}
//初始化加载
static {
try {
props.load(BaseDAO.class.getClassLoader().getResourceAsStream("jdbc.properties"));
} catch (IOException e) {
log.info("加载jdbc.properties配置文件异常,请检查!");
}
url =(props.getProperty("url"));
username = (props.getProperty("username"));
password = (props.getProperty("password"));
driver=(props.getProperty("driverClassName"));
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
log.info("加载数据库驱动异常,请检查!");
}
}
//打开数据库连接
public Connection getConnection() {
try {
Class.forName(driver);
conn = (Connection) DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
System.out.println("加载驱动出现异常!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭数据库连接
public void closeConnection(Connection conn) {
if (conn == null) return;
try {
if (!conn.isClosed()) {
//关闭数据库连接
conn.close();
}
} catch (SQLException e) {
log.info("关闭数据库连接发生异常,请检查!");
}
}
//查询
public ResultSet extuceQuery(String sql, Object[] values) {
conn = getConnection();
try {
pstmt = conn.prepareStatement(sql);
setValue(values);
rs = pstmt.executeQuery();
} catch (SQLException e) {
System.out.println("查询数据出现异常!");
e.printStackTrace();
}
return rs;
}
//添加 修改 删除
public boolean extuceUpdate(String sql, Object[] values) {
getConnection();
boolean b = false;
try {
//
pstmt = conn.prepareStatement(sql);
setValue(values);
int i = pstmt.executeUpdate();
if (i >= 1) {
b = true;
}
} catch (SQLException e) {
System.out.println("修改数据出现异常!");
e.printStackTrace();
}
return b;
}
//调用存储过程的方法(无返回值)
public String callProcedure(String sql,Object[] values,int index){//index为输出参数的位置
getConnection();
String flag = null;
try {
call= conn.prepareCall(sql);
setValues(values);
call.registerOutParameter(index, String);
call.executeUpdate();
flag=call.getString(index);
System.out.println(call.toString());
} catch (SQLException e) {
e.printStackTrace();
}finally{
close();
}
return flag;
}
//关闭所有
public void close() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (call != null) {
try {
call.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
if (conn != null && (!conn.isClosed())) {
conn.close();
}
} catch (SQLException e) {
}
}
}
//关闭所有
public void closeAll() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
if (conn != null && (!conn.isClosed())) {
conn.close();
}
} catch (SQLException e) {
}
}
}
//为占位符赋值
public void setValue(Object[] values) {
if (values != null && values.length != 0)
for (int i = 0; i < values.length; i++) {
try {
pstmt.setObject(i + 1, values[i]);
} catch (SQLException e) {
System.out.println("为占位符赋值出现异常!");
e.printStackTrace();
}
}
}
//为存储过程中的占位符赋值
public void setValues(Object[] values) {
if (values != null && values.length != 0)
for (int i = 0; i < values.length; i++) {
try {
call.setObject(i + 1, values[i]);
} catch (SQLException e) {
System.out.println("为存储过程的占位符赋值出现异常!");
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String sql="call userDepositActive2(?,?,?,?)";
Object values[]={1,3,200};
System.out.println(getInstance().callProcedure(sql, values,4));
// String sql1="call userDepositActive1(?)";
//
// System.out.println(getInstance().callProcedure(sql1, null,1));
}
}