📄 dbutil.java
字号:
//Source file: D:\\WORK\\1G项目实践\\WSMS\\CI\\CODE\\src\\com\\intacpurun\\wsms\\comm\\db\\DbUtil.java
package com.intacpurun.wsms.comm.db;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import com.intacpurun.wsms.comm.*;
/**
* 数据库数据操作工具类
*/
public class DbUtil
{
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
/**
* @roseuid 425337030242
*/
private DbUtil()
{
conn = getConnection();
}
/**
* @return cn.ac.ia.hitic.platform.dao.DbUtil
* @roseuid 426DDEE80186
*/
public static DbUtil open()
{
return new DbUtil();
}
/**
* @return boolean
* @roseuid 426DDEED0213
*/
public boolean close()
{
boolean isClose = true;
//关闭ResultSet
try
{
if (rs != null)
{
rs.close();
rs = null;
}
} catch (SQLException ex)
{
rs = null;
isClose = false;
System.out.println("error happened when closing ResultSet " +
ex.getMessage());
}
//关闭PreparedStatement
try
{
if (pstmt != null)
{
pstmt.close();
pstmt = null;
}
} catch (SQLException ex1)
{
pstmt = null;
isClose = false;
System.out.println("error happened when closing preparedstatement " +
ex1.getMessage());
}
//关闭Connection
try
{
if (conn != null)
{
conn.close();
conn = null;
}
} catch (SQLException ex2)
{
conn = null;
isClose = false;
System.out.println("error happened when closing connection " +
ex2.getMessage());
}
return isClose;
}
/**
* 执行查询语句,获得查询接口
* @param sql
* @return ResultSet
* @roseuid 4251F05D009C
*/
public ResultSet executeQuery(String sql)
{
System.out.println("conn>...:" + conn);
try
{
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
} catch (Exception e)
{
System.out.println("error sql ===>" + sql);
System.out.println(e.getMessage());
e.printStackTrace();
close();
}
return rs;
}
/**
* 执行修改语句,返回修改接口
* @param sql
* @return int
* @roseuid 4251F08B00AB
*/
public int executeUpdate(String sql)
{
int result = -1;
try
{
pstmt = conn.prepareStatement(sql);
result = pstmt.executeUpdate();
} catch (Exception e)
{
System.out.println("error sql ===>" + sql);
System.out.println(e);
e.printStackTrace();
close();
}
return result;
}
/**
* 获得数据库链接
* @return Connection
* @roseuid 4251F0090148
*/
private Connection getConnection()
{
Connection conn= null;
try
{
// Context context = (Context) (new InitialContext()).lookup("java:comp/env");
// DataSource ds = (DataSource) context.lookup(Constants.WSMS_DATASOURCE);
// this.conn = ds.getConnection();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:"+ Constants.WSMS_DATASOURCE,"wsms","wsms");
} catch (Exception ex)
{
System.out.println(ex);
ex.printStackTrace();
}
return conn;
}
/**
* 提交并释放连接
*/
public boolean commit()
{
boolean isCommit = true;
try
{
conn.commit();
} catch (Exception e)
{
isCommit = false;
System.out.println("commit failed " + e.getMessage());
e.printStackTrace();
} finally
{
close();
}
return isCommit;
}
/**
* 修改提交方式为手动提交
*/
public boolean setAutoCommitFalse()
{
boolean isSetOK = true;
try
{
conn.setAutoCommit(false);
} catch (Exception e)
{
isSetOK = false;
close();
System.out.println("setAutoCommit failed " + e.getMessage());
e.printStackTrace();
}
return isSetOK;
}
/**
* 回滚并释放连接
*/
public boolean rollBack()
{
boolean isRollBack = true;
try
{
conn.rollback();
} catch (Exception e)
{
isRollBack = false;
System.out.println("rollbak failed " + e.getMessage());
e.printStackTrace();
}
return isRollBack;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -