JDBC连接数据库帮助类


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


/**
* 连接数据包装类
*
* @author yanxiaojia
* @qq 1576735050
*
*/
public class BaseDao {

public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
public static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
public static final String USERNAME = "scott";
public static final String PASSWORD = "tiger";

/**
* 连接数据
*
* @return conn
*/
public Connection getConnection() {
Connection conn = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

/**
* 关闭连接对象
*
* @param conn
* 连接对象
* @param pstmt
* 预编译对象
* @param rs
* 结果集
*/
public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
pstmt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 增删改操作
*
* @param sql
* SQL命令
* @param param
* 参数
* @return
*/
public int executsQuery(String sql, String[] param) {
int result = 0;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = this.getConnection();
pstmt = conn.prepareStatement(sql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
pstmt.setString(i + 1, param[i]);
}
}
result = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
this.closeAll(conn, pstmt, null);
}
return result;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值