📄 j2eedatabase.java~5~
字号:
package com.j2ee.database;
import com.j2ee.database.ConnFactory;
import java.sql.*;
import javax.naming.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: 天津城市建设学院计算机系</p>
* @author 杨帆
* @version 1.0
* 完成数据库的连接、断开、插入、查询等访问操作
*/
public class J2eedatabase{
public Connection conn = null; //数据库连接
public Statement stmt = null; //建立和执行语句
public ResultSet rs = null; //检索和更新结果
public PreparedStatement ppstmt = null; //预处理准备语句
public DatabaseMetaData dbmeta = null; //数据库和结果集的元数据
public ResultSetMetaData rsmeta = null; //结果元数据信息
//--------行集、存储过程等,在系统需要中再进行添加或进行继承---------//
public Exception dex = null;
public J2eedatabase() {
dex = null;
}
/**
* 获取数据的连接方式
*
* @return boolean
*/
public boolean getConnetion() {
if (conn != null) { //如果连接,返回
return true;
}
boolean isConn = false;
ConnFactory connFactory = new ConnFactory();
try {
if (connFactory.isDataSourceConn()) {
conn = connFactory.getDataSourceConn(); //数据源连接
}
else if (connFactory.isDriverManageConn()) {
conn = connFactory.getDriverManageConn(); //静态连接
}
else if (connFactory.isODBCJDBCConn()) {
conn = connFactory.getODBCJDBCConn(); //ODBC连接
}
else {
conn = connFactory.getDataPoolConn(); //连接池及其他连接
}
}
catch (SQLException ex) {
//日志处理
dex = ex;
return false;
}
catch (NamingException ex1) {
dex = ex1;
return false;
}
catch (ClassNotFoundException ex2) {
dex = ex2;
return false;
}
if (conn != null) {
isConn = true;
}
return isConn;
}
//查询数据库数据,输入参数是数据库访问SQL语句
public boolean researchDB(String sqlstr_research) {
String sqlstr = sqlstr_research;
boolean returnValue = false;
if (getConnetion()) {
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlstr);
returnValue = true;
return returnValue;
}
catch (SQLException ex) {
//-----------数据库连接报错处理--------------//
dex = ex;
return returnValue;
}
}
else {
return returnValue;
}
}
//查询数据库数据,输入参数是数据库访问SQL语句,与上面的函数功能一样
public boolean selectDB(String sqlstr_select) {
String sqlstr = sqlstr_select;
boolean returnValue = false;
if (getConnetion()) {
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlstr);
returnValue = true;
return returnValue;
}
catch (SQLException ex) {
//-----------数据库连接报错处理--------------//
dex = ex;
return returnValue;
}
}
else {
return returnValue;
}
}
//添加数据库数据,添加记录
public boolean insertDB(String sqlstr_insert) {
String sqlstr = sqlstr_insert;
boolean returnValue = false;
if (getConnetion()) {
try {
stmt = conn.createStatement();
stmt.executeUpdate(sqlstr);
returnValue = true;
return returnValue;
}
catch (SQLException ex) {
//-----------数据库连接报错处理--------------//
dex = ex;
return returnValue;
}
}
else {
return returnValue;
}
}
/**
* 添加数据库数据,按照准备语句添加记录
*
* @param sqlstr_insert String
* @return boolean
*/
public boolean insertDBPrepare(String sqlstr_insert) {
String sqlstr = sqlstr_insert;
boolean returnValue = false;
if (getConnetion()) {
try {
ppstmt = conn.prepareStatement(sqlstr);
ppstmt.executeUpdate(sqlstr);
returnValue = true;
return returnValue;
}
catch (SQLException ex) {
//-----------数据库连接报错处理--------------//
dex = ex;
return returnValue;
}
}
else {
return returnValue;
}
}
//删除数据库记录
public boolean deleteDB(String sqlstr_delete) {
String sqlstr = sqlstr_delete;
boolean returnValue = false;
if (getConnetion()) {
try {
stmt = conn.createStatement();
stmt.executeUpdate(sqlstr);
returnValue = true;
return returnValue;
}
catch (SQLException ex) {
//-----------数据库连接报错处理--------------//
dex = ex;
return returnValue;
}
}
else {
return returnValue;
}
}
public boolean deleteDB(String[] sqlstr_delete) {
String[] sqlstr = sqlstr_delete;
int sqlcount=sqlstr_delete.length;
int i;
boolean returnValue = false;
if (getConnetion()) {
try {
stmt = conn.createStatement();
for(i=0;i<sqlcount;i++)
stmt.addBatch(sqlstr[i]);
stmt.executeBatch();
returnValue = true;
return returnValue;
}
catch (SQLException ex) {
//-----------数据库连接报错处理--------------//
dex = ex;
return returnValue;
}
}
else {
return returnValue;
}
}
//修改数据库记录
public boolean updateDB(String sqlstr_update) {
String sqlstr = sqlstr_update;
boolean returnValue = false;
if (getConnetion()) {
try {
stmt = conn.createStatement();
stmt.executeUpdate(sqlstr);
returnValue = true;
return returnValue;
}
catch (SQLException ex) {
//-----------数据库连接报错处理--------------//
dex = ex;
return returnValue;
}
}
else {
return returnValue;
}
}
//是否存在所访问的数据库记录
public boolean isExitInDB(String sqlstr_isexist) {
String sqlstr = sqlstr_isexist;
boolean returnValue = false;
if (getConnetion()) {
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlstr);
if (rs.next()) {
returnValue = true;
}
return returnValue;
}
catch (SQLException ex) {
//-----------数据库连接报错处理--------------//
dex = ex;
return returnValue;
}
}
else {
return returnValue;
}
}
//数据库关闭
public boolean closeDB() {
boolean returnValue = false;
try {
if (rs != null) {
rs.close();
rs=null;
}
if (stmt != null) {
stmt.close();
stmt=null;
}
if (ppstmt != null) {
ppstmt.close();
ppstmt=null;
}
if (conn != null) {
conn.close();
conn=null;
}
returnValue = true;
return returnValue;
}
catch (SQLException ex) {
//-----------数据库连接报错处理--------------//
dex = ex;
return returnValue;
}
}
/**
* BeginTran
*/
public boolean BeginTran() {
if(getConnetion()){
try {
conn.setAutoCommit(false);
} catch (SQLException ex) {
dex=ex;
return false;
}
return true;
}
return false;
}
/**
* CommitDB()
*/
public boolean CommitDB() {
try {
conn.commit();
} catch (SQLException ex) {
dex=ex;
return false;
}
return true;
}
/**
* RollbackDB
*/
public boolean RollbackDB() {
try {
conn.commit();
} catch (SQLException ex) {
dex=ex;
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -