/*
* Created on 2004-9-18
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package oa.sys;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
/**
****************************************************
*绫诲悕绉帮細 Db<br>
*绫诲姛鑳斤細 鏁版嵁搴撴搷浣\uFFFD<br>
*鍒涘缓锛\uFFFD鐧戒紵鏄\uFFFD2004骞\uFFFD鏈\uFFFD3鏃\uFFFDbr>
****************************************************
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Db {
private Connection con;
private Statement stmt;
private ResultSet rs;
private PreparedStatement pstmt;
public static int error=0;
/***************************************************
*鍑芥暟鍚嶇О锛\uFFFDgetCon()<br>
*鍑芥暟鍔熻兘锛\uFFFD鑾峰彇鏁版嵁搴撹繛鎺\uFFFDbr>
*杩斿洖鍊硷細 鏃\uFFFDbr>
*鍙傛暟璇存槑锛\uFFFD鏃\uFFFDbr>
*鍒涘缓锛\uFFFD 鐧戒紵鏄\uFFFD2004骞\uFFFD鏈\uFFFD2鏃\uFFFD
****************************************************/
public static synchronized Connection getCon()throws Exception{
Context ctx;
DataSource ds;
try{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/OA");
System.err.println();
System.err.println("鏁版嵁杩炴帴+"+(++error));
return ds.getConnection();
}catch(SQLException e){
System.out.print(e);
throw e;
}
catch(NamingException e){
System.out.print(e);
throw e;
}
}
/***************************************************
*鍑芥暟鍚嶇О锛\uFFFDgetStmtread()<br>
*鍑芥暟鍔熻兘锛\uFFFD鑾峰彇鏁版嵁搴撻泦鍚\uFFFDbr>
*杩斿洖鍊硷細 Statement
* stmt:杩斿洖鏁版嵁搴撻泦鍚堝彧鐢ㄤ簬SELECT璇彞<br>
*鍙傛暟璇存槑锛\uFFFD鏃\uFFFDbr>
*鍒涘缓锛\uFFFD 鐧戒紵鏄\uFFFD2004骞\uFFFD鏈\uFFFD2鏃\uFFFD
****************************************************/
public Statement getStmtread(){
try{
con=getCon();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}catch(Exception e){
System.out.println("getStmtread");
System.out.println(e.getMessage());
}
return stmt;
}
/***************************************************
*鍑芥暟鍚嶇О锛\uFFFDgetRowCount()<br>
*鍑芥暟鍔熻兘锛\uFFFD杩斿洖琛ㄧ殑琛屾暟<br>
*杩斿洖鍊硷細 int count<br>
* count>0杩斿洖鐨勮鏁\uFFFDbr>
* count=-1琛ㄥ悕涓嶅瓨鍦ㄦ垨娌℃湁绾綍<br>
*鍙傛暟璇存槑锛\uFFFDString sql<br>
* sql 鏁版嵁搴撲腑鐨勮〃鍚\uFFFD
*鍒涘缓锛\uFFFD 鐧戒紵鏄\uFFFD2004骞\uFFFD0鏈\uFFFD鏃\uFFFD
****************************************************/
public int getRowCount(String sql){
int count=0;;
try{
stmt=this.getStmtread();
rs=stmt.executeQuery("SELECT COUNT(*) FROM "+sql);
rs.getMetaData();
if(rs.next()){
count=rs.getInt(1);
}else{
count=-1;
}
}catch(Exception e){
System.out.println("getRowCount");
System.out.println(e.getMessage());
count=-2;
}finally{
this.close();
}
return count;
}
/***************************************************
*鍑芥暟鍚嶇О锛\uFFFDgetStmt()<br>
*鍑芥暟鍔熻兘锛\uFFFD鑾峰彇鏁版嵁搴撻泦鍚\uFFFDbr>
*杩斿洖鍊硷細 Statement
* stmt:杩斿洖鏁版嵁搴撻泦鍚堜笉鑳界敤浜嶴ELECT璇彞<br>
*鍙傛暟璇存槑锛\uFFFD鏃\uFFFDbr>
*鍒涘缓锛\uFFFD 鐧戒紵鏄\uFFFD2004骞\uFFFD鏈\uFFFD2鏃\uFFFD
****************************************************/
public Statement getStmt(){
try{
con=getCon();
stmt=con.createStatement();
}catch(Exception e){
System.out.println("getStmt");
System.out.println(e.getMessage());
}
return stmt;
}
/***************************************************
*鍑芥暟鍚嶇О锛\uFFFDIdtoName()<br>
*鍑芥暟鍔熻兘锛\uFFFD鏍规嵁鍛樺伐ID鑾峰彇鍛樺伐濮撳悕<br>
*杩斿洖鍊硷細 String
* String:杩斿洖鍛樺伐濮撳悕<br>
*鍙傛暟璇存槑锛\uFFFD鏃\uFFFDbr>
*鍒涘缓锛\uFFFD 鐧戒紵鏄\uFFFD2004骞\uFFFD1鏈\uFFFD鏃\uFFFD
****************************************************/
public String IdtoName(int id){
String name=null;
try{
stmt=this.getStmtread();
rs=stmt.executeQuery("SELECT name FROM eminfo WHERE employeeid="+id);
if(rs.next()){
name=rs.getString(1);
Str str=new Str();
name=str.outStr(name);
}else{
name="琚垹闄\uFFFD;
}
this.close();
}catch(Exception e){
System.out.println("IdtoName");
System.out.println(e.getMessage());
}
return name;
}
/***************************************************
*鍑芥暟鍚嶇О锛\uFFFDIdtoDeName()<br>
*鍑芥暟鍔熻兘锛\uFFFD鏍规嵁鍛樺伐ID鑾峰彇鍛樺伐閮ㄩ棬鍚嶇О<br>
*杩斿洖鍊硷細 String
* String:杩斿洖鍛樺伐閮ㄩ棬鍚嶇О<br>
*鍙傛暟璇存槑锛\uFFFD鏃\uFFFDbr>
*鍒涘缓锛\uFFFD 鐧戒紵鏄\uFFFD2004骞\uFFFD1鏈\uFFFD鏃\uFFFD
****************************************************/
public String IdtoDeName(int id){
String name=null;
try{
stmt=this.getStmtread();
rs=stmt.executeQuery("SELECT name FROM department WHERE departmentid="+id);
if(rs.next()){
name=rs.getString(1);
Str str=new Str();
name=str.outStr(name);
}else{
name="鏃犳閮ㄩ棬";
}
this.close();
}catch(Exception e){
System.out.println("IdtoName");
System.out.println(e.getMessage());
}
return name;
}
/***************************************************
*鍑芥暟鍚嶇О锛\uFFFDIdtoDo()<br>
*鍑芥暟鍔熻兘锛\uFFFD鏍规嵁杈撳叆鏉′欢鑾峰彇鍗曞瓧娈典竴鏉℃暟鎹\uFFFDbr>
*杩斿洖鍊硷細 String
* String:杩斿洖鍛樺伐閮ㄩ棬鍚嶇О<br>
*鍙傛暟璇存槑锛\uFFFDfield 瑕佹煡璇㈢殑鍗曞瓧娈靛悕绉\uFFFDbr>
* from 瑕佹煡璇㈢殑琛ㄥ悕绉\uFFFDbr>
*鍒涘缓锛\uFFFD 鐧戒紵鏄\uFFFD2004骞\uFFFD1鏈\uFFFD鏃\uFFFD
****************************************************/
public String IdtoDo(String field,String from){
String name=null;
try{
stmt=this.getStmtread();
rs=stmt.executeQuery("SELECT "+field+" FROM "+from);
if(rs.next()){
name=rs.getString(1);
Str str=new Str();
name=str.outStr(name);
}
this.close();
}catch(Exception e){
System.out.println("IdtoDo");
System.out.println(e.getMessage());
}
return name;
} /***************************************************
*鍑芥暟鍚嶇О锛\uFFFDgetPstmt()<br>
*鍑芥暟鍔熻兘锛\uFFFD鑾峰彇鏁版嵁搴撻泦鍚\uFFFDbr>
*杩斿洖鍊硷細 PreparedStatement
* pstmt:杩斿洖鏁版嵁搴撻澶勭悊璇彞<br>
*鍙傛暟璇存槑锛\uFFFDsql 绫籹ql璇彞<br>
*鍒涘缓锛\uFFFD 鐧戒紵鏄\uFFFD2004骞\uFFFD鏈\uFFFD2鏃\uFFFD
****************************************************/
public PreparedStatement getPstmt(String sql){
try{
con=getCon();
pstmt=con.prepareStatement(sql);
}catch(Exception e){
System.out.println("getPstmt");
System.out.println(e.getMessage());
}
return pstmt;
}
/***************************************************
*鍑芥暟鍚嶇О锛歝lose()<br>
*鍑芥暟鍔熻兘锛氬叧闂暟鎹簱杩炴帴<br>
*杩斿洖鍊硷細 void<br>
*鍙傛暟璇存槑锛氭棤<br>
*鏈\uFFFD垨淇敼锛氱櫧浼熸槑
* 2004骞\uFFFD鏈\uFFFD鏃\uFFFD
****************************************************/
public void close(){
try{
if(rs!=null)rs.close();
}catch(Exception e){
}
try{
if(stmt!=null)stmt.close();
}catch(Exception e){
}
try{
if(con!=null){
con.close();
con=null;
System.err.println();
System.err.println("鏁版嵁杩炴帴-"+(--error));
}
}catch(Exception e){
System.out.println("close");
System.out.println(e.getMessage());
}
}
}
- 1
- 2
- 3
- 4
前往页