⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 db.java

📁 随着信息技术的飞速发展和日趋激烈的商业竞争
💻 JAVA
字号:

package oa.sys;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;

public class Db {
	private Connection con;
	private Statement stmt;
	private ResultSet rs;
	private PreparedStatement pstmt;
	public static int error=0;
	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;
		}
	}
	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;
	}
	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;
	}
	public Statement getStmt(){
		try{
			con=getCon();
			stmt=con.createStatement();
		}catch(Exception e){
			System.out.println("getStmt");
			System.out.println(e.getMessage());
		}
		return stmt;
	}
	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="被删除";
			}
			this.close();
		}catch(Exception e){
			System.out.println("IdtoName");
			System.out.println(e.getMessage());
		}
		return name;
	}
	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;
	}
	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;
	}
	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;
	}
	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());
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -