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

📄 unitda.java

📁 医药供应链管理系统
💻 JAVA
字号:
package com.captainli.dboperation;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.captainli.bean.UnitBean;
import com.captainli.struts.form.UnitForm;
import com.captainli.util.GetConnection;

/**
 * unit表数据库操作类
 * @author CaptainLi
 *
 */
public class UnitDA {
	private Connection conn = GetConnection.getConn();
	private PreparedStatement pstmt = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	/**
	 * 关闭数据库对象
	 *
	 */
	public void closeDB(){
		try {
			if(rs != null){
				rs.close();
			}
			if(stmt != null){
				stmt.close();
			}
			if(pstmt != null){
				pstmt.close();
			}
			if(conn != null){
				conn.close();
			}
		} catch (Exception e) {
			e.getStackTrace();
		}
	}
	/**
	 * 返回所有计量单位
	 * @return
	 */
	public ArrayList selectUnit(){
		ArrayList arry = new ArrayList();
		UnitBean bean = null;
		String sql = "select * from unit order by u_id";
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				bean = new UnitBean(rs.getInt("u_id"), rs.getString("u_name"));
				arry.add(bean);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeDB();
		}
		return arry;
	}
	/**
	 * 通过ID返回计量单位
	 * @param u_id
	 * @return
	 */
	public String selectUnitByID(int u_id){
		String tmp = "";
		String sql = "select u_name from unit where u_id = ?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, u_id);
			rs = pstmt.executeQuery();
			if(rs.next()){
				tmp = rs.getString("u_name");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeDB();
		}
		return tmp;
	}
	/**
	 * 添加计量单位
	 * @param form
	 */
	public void addUnit(UnitForm form){
		String sql = "insert into unit (u_name) values (?)";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, form.getU_name());
			pstmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeDB();
		}
	}
	/**
	 * 修改计量单位
	 */
	public void updateUnit(UnitForm form, int u_id){
		String sql = "update unit set u_name = ? where u_id = ?";
		try {
			pstmt= conn.prepareStatement(sql);
			pstmt.setString(1, form.getU_name());
			pstmt.setInt(2, u_id);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeDB();
		}
	}
}

⌨️ 快捷键说明

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