设为首页 加入收藏

TOP

Java jdbc抽象实现类实例(一)
2014-11-24 02:22:35 来源: 作者: 【 】 浏览:2
Tags:Java jdbc 抽象 实现 实例

package com.loong.mail.crontab;


import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;


import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 数据库连接
* @author lining
*
*/
public abstract class BaseDaoImpl {
private static final Log log = LogFactory.getLog(BaseDaoImpl.class);


public Connection con = null;


private Statement stm = null;


public BaseDaoImpl() {
getConnection();
}


public void getConnection() {
// Connection conn = null;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx .lookup("java:comp/env/jdbc/mysql");
con = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}



/*public void getConnection() {
try {

// con = DriverManager.getConnection(
// "jdbc:mysql://192.168.100.21:3306/volume user=webbycmc&password=Root123");
con = DriverManager.getConnection(
"jdbc:mysql://192.168.5.74:3306/volume user=root&password=123456");
System.out.println("连接成功"+con);
} catch (SQLException e) {
e.printStackTrace();
log.error(e);
}
log.info("连接数据库成功");
}
*/
public int update(String sql) {
int result = 0;
PreparedStatement preparedStatement;
try {
preparedStatement = con.prepareStatement(sql);
result = preparedStatement.executeUpdate();
} catch (SQLException e) {
log.error(e);
}
return result;
}


public ResultSet executeQuery(Connection con, String sql) {
ResultSet result = null;
PreparedStatement preparedStatement;
try {
preparedStatement = con.prepareStatement(sql);
result = preparedStatement.executeQuery();
} catch (SQLException e) {
log.error(e);
}
return result;
}


public ResultSet executeQuery(String sql) {
ResultSet result = null;
PreparedStatement preparedStatement;
try {
preparedStatement = con.prepareStatement(sql);
result = preparedStatement.executeQuery();
} catch (SQLException e) {
log.error(e);
}
return result;
}


public void createStatement() {
try {
stm = con.createStatement();
} catch (SQLException e) {
}
}


private CallableStatement cstmt = null;


public void createCallStatement(String sql) {


try {
cstmt = con.prepareCall(sql);
} catch (SQLException e) {
log.error(e);
}
}


public void execute(String sql) {
if (cstmt != null) {
try {
if (sql != null) {
cstmt.execute(sql);
} else {
cstmt.execute();
}
} catch (SQLException e) {
log.error(e);
}
}
}


public void addBatch(String sql) {
if (stm != null) {
try {
stm.addBatch(sql);
} catch (SQLException e) {
}
}
}


public void executeBatch() {
if (stm != null) {
try {
stm.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
}
}
}


public int getTotalCount(String sql) {
int totalCount = 0;
ResultSet result = null;
PreparedStatement preparedStatement;
try {
preparedStatement = con.prepareStatement(sql);
result = preparedStatement.executeQuery();
if (result.next()) {
totalCount = result.getInt(1);
}
} catch (SQLException e) {
log.error(e);
} finally {
closeResultSet(result);
}
return totalCo

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux 下定时器的实现方式分析 下一篇Eclipse开发JAVA 配置的境变量的..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: