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

📄 trabean.java

📁 完整的JAVA工程
💻 JAVA
字号:
package employee;

import java.util.*;
import java.sql.*;
import javax.swing.*;

/**
 * 有关培训信息数据库操作的类
 */
public class TraBean {
	String sql;
	ResultSet rs = null;

	String tTid;
	String tName;
	String tContent;
	String sNum;

	String colName;//列名
	String colValue;//列值
	int traId;//新培训的编号
	
	/**
	 * 添加培训信息
	 */
	public void traAdd(String tname, String tcontent){
		
		Database DB = new Database();
		this.tName = tname;
		this.tContent = tcontent;

		if(tName == null||tName.equals("")){
			JOptionPane.showMessageDialog(null, "请输入培训名称", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		else{
			sql = "insert into train(tname,tcontent) values ('"+tName+"','"+tContent+"')";
			try{
				DB.OpenConn();
				DB.executeUpdate(sql);
				JOptionPane.showMessageDialog(null,"成功添加一条新的纪录!");

			}
			catch(Exception e){
				JOptionPane.showMessageDialog(null, "保存失败", "错误", JOptionPane.ERROR_MESSAGE); 
			}
			finally {
				DB.closeStmt();
				DB.closeConn();
			}
		}
	}

	/**
	 * 修改培训信息
	 */
	public void traModify(String tid, String tname, String tcontent){
		Database DB = new Database();
		this.tTid = tid;
		this.tName = tname;
		this.tContent = tcontent;

		if(tName == null||tName.equals("")){
			JOptionPane.showMessageDialog(null, "请输入培训名称", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		else{
			sql = "update train set tname = '"+tName+"', tcontent = '"+tContent+"' where tid = "+Integer.parseInt(tTid)+"";
			try{
				DB.OpenConn();
				DB.executeUpdate(sql);
				JOptionPane.showMessageDialog(null,"成功修改一条新的纪录!");
			}
			catch(Exception e){
				System.out.println(e);
				JOptionPane.showMessageDialog(null, "更新失败", "错误", JOptionPane.ERROR_MESSAGE); 
			}
			finally {
				DB.closeStmt();
				DB.closeConn();
			}
		}
	}

	/**
	 * 删除培训信息
	 */
	public void traDel(String tname){
		Database DB = new Database();
		this.tName = tname;
		
		sql = "delete from train where tname = '"+tName+"'";
		try{
			DB.OpenConn();
			DB.executeUpdate(sql);
			JOptionPane.showMessageDialog(null,"成功删除一条新的纪录!");
		}
		catch(Exception e){
			System.out.println(e);
			JOptionPane.showMessageDialog(null, "删除失败", "错误", JOptionPane.ERROR_MESSAGE); 
		}
		finally {
			DB.closeStmt();
			DB.closeConn();
		}
	}
	
	/**
	 * 根据培训名称,搜索培训的相关信息
	 */
	public String[] traSearch(String tname){
		Database DB = new Database();
		this.tName = tname;
		String[] s = new String[2];

		sql = "select * from train where tname = '"+tName+"'";
		try{
			DB.OpenConn();
			rs = DB.executeQuery(sql);
			if(rs.next()){
				s[0] = rs.getString("tid");
				s[1] = rs.getString("tcontent");
			}
			else
				s = null;
		}
		catch(Exception e){
		}
		finally {
			DB.closeStmt();
			DB.closeConn();
		}
		return s;
	}

	/**
	 * 根据培训名称,搜索培训信息
	 */
	public String[] traNameSear(String tname){
		Database DB = new Database();
		String[] s = new String[3];
		this.tName = tname;
		DB.toGBK(tName);

		sql = "select * from train where tname = '"+tName+"'";
		try{
			DB.OpenConn();
			rs = DB.executeQuery(sql);
			if(rs.next()){
				s[0] = rs.getString("tid");
				s[1] = rs.getString("tname");
				s[2] = rs.getString("tcontent");
			}
			else
				s = null;
		}
		catch(Exception e){
			System.out.println(e);
		}
		finally {
			DB.closeStmt();
			DB.closeConn();
		}
		return s;
	}
	
	/**
	 * 培训信息综合查询
	 */
	public String[][] traAllSearch(String colname,String colvalue){
		this.colName = colname;
		this.colValue = colvalue;
		
		Database DB = new Database();
		String[][] cn = null;
		int row = 0;
		int i = 0;
		
		if(colValue == null||colValue.equals("")){
			sql = "select * from train";
		}
		else{
			sql = "select * from train where "+colName+" = '"+colValue+"'";
		}

		try{
			DB.OpenConn();
			rs = DB.executeQuery(sql);
			if(rs.last()){
				row = rs.getRow();
			}
			if(row == 0){
				cn = null;
			}
			else{
				cn = new String[row][3];
				rs.first();
				rs.previous();
				while(rs.next()){
					cn[i][0] = rs.getString("tid");
					cn[i][1] = rs.getString("tname");
					cn[i][2] = rs.getString("tcontent");
					i++;
				}
			}
		}
		catch(Exception e){
			System.out.println(e);
		}
		finally {
			DB.closeStmt();
			DB.closeConn();
		}
		return cn;
	}
	/**
	 * 获得train表中的所有培训名称
	 */
	public String[] getAllName(){
		
		String[] s = null;
		int row = 0;
		int i = 0;
		Database DB = new Database();

		sql = "select tname from train";
		try{
			DB.OpenConn();
			rs = DB.executeQuery(sql);
			if(rs.last()){
				row = rs.getRow();
			}
			if(row == 0){
				s = null;
			}
			else{
				s = new String[row];
				rs.first();
				rs.previous();
				while(rs.next()){
					s[i] = rs.getString(1);
					i++;
				}
			}
		}
		catch(Exception e){
			System.out.println(e);
		}
		finally {
			DB.closeStmt();
			DB.closeConn();
		}
		return s;
	}
}

⌨️ 快捷键说明

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