package dao
import java.sql.*
/**
*
* @author Administrator
*数据库连接
*/
public class BaseDao {
//连接字符串
public String driver="oracle.jdbc.driver.OracleDriver"//数据库驱动
public String url="jdbc:oracle:thin:@localhost:1521:hfaccp"//建立到给定数据库 URL 的连接。
public String username="system"//数据库用户
public String password="system"//数据库密码
//声明接口
public Connection con
public PreparedStatement pstmt
public ResultSet rs
//获得数据库连接
public Connection getConnection()
{
try {
Class.forName(driver)
con=DriverManager.getConnection(url,username,password)
} catch (ClassNotFoundException e) {
e.printStackTrace()
} catch (SQLException e) {
e.printStackTrace()
}
return con
}
//释放数据库资源
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()
}
}
if(con!=null)
{
try {
con.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
}
}
public static int executeSQL(String preparedSql, Object[] param) {Connection conn = null
PreparedStatement pstmt = null
int num = 0
try {
conn = getConnectionForJndi()
pstmt = conn.prepareStatement(preparedSql)
if (param != null) {
for (int i = 0i <param.lengthi++) {
pstmt.setObject(i + 1, param[i])
}
num = pstmt.executeUpdate()
}
} catch (SQLException e) {
e.printStackTrace()
} catch (DBAccessException e) {
e.printStackTrace()
} finally {
closeAll(conn, pstmt, null)//释放资源
}
return num
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)