📄 db.java
字号:
package oa.sys;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
/**
****************************************************
*类名称: Db<br>
*类功能: 数据库操做<br>
****************************************************
*/
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("jdbcoa");
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 static Connection getCon() throws Exception{
try{
return ConnectionFactory.getConnection("jdbcoa");
}catch(SQLException x){
System.out.println(x);
throw x;
}catch(NamingException x){
System.out.println(x);
throw x;
}
}
/***************************************************
*函数名称getStmtread()
*函数功能获取数据库集
*返回值: Statement
* stmt:返回数据库集合只用于SELECT语句
****************************************************/
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;
}
/***************************************************
*函数名称getRowCount()
*函数功能返回表的行数
*返回值: int count
* count>0返回的行
* count=-1表名不存在或没有纪录
*参数说明String sql
sql 数据库中的表
****************************************************/
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;
}
/***************************************************
*函数名称getStmt()
*函数功能获取数据库集
*返回值: Statement
* stmt:返回数据库集合不能用于SELECT语句
****************************************************/
public Statement getStmt(){
try{
con=getCon();
stmt=con.createStatement();
}catch(Exception e){
System.out.println("getStmt");
System.out.println(e.getMessage());
}
return stmt;
}
/***************************************************
*函数名称IdtoName()
*函数功能根据员工ID获取员工姓名
*返回值: String
* String:返回员工姓名
****************************************************/
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;
}
/***************************************************
*函数名称IdtoDeName()
*函数功能根据员工ID获取员工部门名称
*返回值: String
* String:返回员工部门名称
****************************************************/
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;
}
/***************************************************
*函数名称IdtoDo()
*函数功能根据输入条件获取单字段一条数
*返回值: String
* String:返回员工部门名称<br>
*参数说明field 要查询的单字段名
* from 要查询的表名
****************************************************/
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;
} /***************************************************
*函数名称getPstmt()
*函数功能获取数据库集
*返回值: PreparedStatement
* pstmt:返回数据库预处理语句
*参数说明sql 类sql语
****************************************************/
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;
}
/***************************************************
*函数名称:close()
*函数功能:关闭数据库连接
*返回值: void
*参数说明:无
****************************************************/
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 + -