chargedao.java

来自「基于jsp的固定资产管理系统,自己做的」· Java 代码 · 共 156 行

JAVA
156
字号
package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import VO.*;

public class ChargeDAO {

	public int insertCharge(Charge c)
	{
		int k=-1;
		ConnectTo conto=new ConnectTo();
		Connection conn=conto.connect();
		PreparedStatement pstm=null;
		String sql="insert into charge values(?,?,?,?)";
		if(conn!=null)
		{
			try{
				String typetemp= c.getCost_type();
				String type=EncodingChange.getReadStr(typetemp);
				pstm=conn.prepareStatement(sql);
				pstm.setString(1,type);
				pstm.setInt(2,c.getAmount());
				pstm.setString(3, c.getOperation());
				pstm.setInt(4, c.getOr_de());
				k=pstm.executeUpdate();
				pstm.close();
				conn.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
				try
				{
					pstm.close();
					conn.close();
				}catch(Exception e1){e1.printStackTrace();}
			}
		}
		return k;
	}
	public int updateCharge(Charge c)
	{
		int k=-1;
		ConnectTo conto=new ConnectTo();
		Connection conn=conto.connect();
		PreparedStatement pstm=null;
		String sql_select="select * from charge where cost_type=?";
		String sql_update="update charge set amount=? where cost_type=?";
		String typetemp= c.getCost_type();
		String type=EncodingChange.getReadStr(typetemp);
		if(conn!=null)
		{
			try{
				pstm=conn.prepareStatement(sql_select);
				pstm.setString(1,type);
				ResultSet rs=pstm.executeQuery();
				if(rs.next())//如果存在type收费类型则更新,否则插入
				{
					pstm.close();
					pstm=conn.prepareStatement(sql_update);
					pstm.setInt(1,c.getAmount());
					pstm.setString(2, type);
					k=pstm.executeUpdate();
					pstm.close();
					conn.close();
				}
				else
				{
					k=this.insertCharge(c);
				}

			}
			catch(Exception e)
			{
				e.printStackTrace();
				try
				{
					pstm.close();
					conn.close();
				}catch(Exception e1){e1.printStackTrace();}
			}
		}
		return k;
	}
	
	
	public int updateOr_de(Charge c)
	{
		int k=-1;
		ConnectTo conto=new ConnectTo();
		Connection conn=conto.connect();
		PreparedStatement pstm=null;
		String sql="update charge set or_de=? where cost_type=?";
		String type=c.getCost_type();
		if(conn!=null)
		{
			try{
				pstm=conn.prepareStatement(sql);
				pstm.setInt(1, c.getOr_de());
				pstm.setString(2,type);
				pstm.executeUpdate();
				pstm.close();
				conn.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
				try
				{
					pstm.close();
					conn.close();
				}catch(Exception e1){e1.printStackTrace();}
			}
		}
		return k;
	}
	
	
	public int chargeInAll()
	{
		ConnectTo conto=new ConnectTo();
		Connection conn=conto.connect();
		PreparedStatement pstm=null;
		int overall=0;
		int k=0;
		String sql="select * from charge where or_de=1";
		if(conn!=null)
		{
			try{
				pstm=conn.prepareStatement(sql);
				ResultSet rs=pstm.executeQuery();
				while(rs.next())
				{
					k=rs.getInt("amount");
					overall=overall+k;
				}
				pstm.close();
				conn.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
				try
				{
					pstm.close();
					conn.close();
				}catch(Exception e1){e1.printStackTrace();}
			}
		}
		return overall;
	}
	
}

⌨️ 快捷键说明

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