?? 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 + -