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

📄 tb_productdaoimpl.java

📁 一个完整的代码管理系统的源代码
💻 JAVA
字号:
/*
 * Copyright (c) 2008-2010 Tanming1003 Inc.
 * All rights reserved.
 * 
 * tanming1003<tanming1003@163.com>
 * 
 * 
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 * Redistributions of source code must retain the above copyright 
 * notice, this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the following disclaimer in the 
 * documentation and/or other materials provided with the distribution. 
 * Neither the name of tanming1003 nor the names of its contributors 
 * may be used to endorse or promote products derived from this 
 * software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */
package hunnu.edu.cn.product.common.db.dao.implement;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
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 java.util.List;

import oracle.jdbc.OracleResultSet;
import oracle.sql.CLOB;

import hunnu.edu.cn.product.common.db.DBProcess;
import hunnu.edu.cn.product.common.db.DBUtil;
import hunnu.edu.cn.product.common.db.DBWrapper;
import hunnu.edu.cn.product.common.db.dao.TB_PRODUCTDao;
import hunnu.edu.cn.product.common.db.model.TB_PRODUCT;

/**
 * @author tanming1003@163.com
 * @date 2008-10-16
 * @time 下午12:37:03
 * @project_name Product
 * @package_name hunnu.edu.cn.product.common.db.dao.implement
 * @file_name    TB_PRODUCTDaoImpl.java
 * @version      1.0
 */
public class TB_PRODUCTDaoImpl implements TB_PRODUCTDao
{

	protected TB_PRODUCTDaoImpl()
	{
		super();
	}
	
	@SuppressWarnings("deprecation")
	public boolean add(TB_PRODUCT product)
	{
		System.out.println("***************");
		DBProcess process=new DBWrapper();
		Connection connection=null;
		String countSql="select pro_id from TB_PRODUCT order by pro_id";
		try
		{
			int totalCount;
			connection=process.getConnection();
			PreparedStatement statement=connection.prepareStatement(countSql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            ResultSet resultSet=statement.executeQuery();
            if(resultSet.last())
            	totalCount=resultSet.getInt(1);
            else
            	totalCount=0;
            totalCount++;
            System.out.println(totalCount);
            String insertSql="insert into TB_PRODUCT values(?,?,?,?,?,?,?,?,?,?,?)";
            statement=connection.prepareStatement(insertSql);
            connection.setAutoCommit(false);
            statement.setInt(1, totalCount);
            statement.setString(2,product.getPro_name());
            statement.setString(3,product.getPro_flg());
            statement.setString(4,product.getChk_ymd());
            statement.setBigDecimal(5, product.getChk_user_id());
            statement.setString(6, product.getIn_ymd());
            statement.setBigDecimal(7, product.getIn_user_id());
            statement.setString(8,product.getPro_comnt());
            statement.setClob(9, oracle.sql.CLOB.getEmptyCLOB());
            statement.setBigDecimal(10,product.getUpd_user_id());
            statement.setTimestamp(11,product.getUpd_ymdhms().timestampValue());
            statement.executeUpdate();
            String querySql="select pro_inf from TB_PRODUCT where pro_id="+totalCount+" for update";
            statement=connection.prepareStatement(querySql);
            resultSet=statement.executeQuery();
            if(resultSet.next())
            {
            	System.out.println("&&&&&&&&&&&&&&&&&&&");
            	CLOB clob = ((OracleResultSet)resultSet).getCLOB(1);
            	clob.putString(1,product.getPro_inf());
            	String sql="update TB_PRODUCT set pro_inf=? where pro_id="+totalCount;
            	statement=connection.prepareStatement(sql);
            	statement.setClob(1, clob);
            	statement.executeUpdate();
            }
            connection.commit();
            statement.close();
            return true;
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			return false;
		}
		finally
		{
			try
			{
				if(connection!=null)
				   process.closeConnection(connection);
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	public int getIdByName(String productName)
	{
		DBProcess process=new DBWrapper();
		Connection connection=null;
		String queryString="select pro_id from TB_PRODUCT where pro_name='"+productName+"'";
		System.out.println(queryString);
		int pro_id=0;
		try 
		{
			connection=process.getConnection();
			Statement statement=connection.createStatement();
			ResultSet set=statement.executeQuery(queryString);
			if(set.next())
			{
				pro_id=set.getInt("pro_id");
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		System.out.println("&&&&&&&&&&&&&&&&");
		System.out.println(pro_id);
		System.out.println("&&&&&&&&&&&&&&&&");
		return pro_id;
	}

	public List<TB_PRODUCT> getAll()
	{
		DBProcess process=new DBWrapper();
		Connection connection=null;
		String sql="select pro_id from TB_PRODUCT";
		List<TB_PRODUCT> result=new ArrayList<TB_PRODUCT>();
		try
		{
			connection=process.getConnection();
			PreparedStatement statement=connection.prepareStatement(sql);
			ResultSet rs=statement.executeQuery();
			while(rs.next())
			{
				TB_PRODUCT product=getByID(rs.getInt(1));
				result.add(product);
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	
		return result;
	}

	public TB_PRODUCT getByID(int id)
	{
		DBProcess process=new DBWrapper();
		Connection connection=null;
		String sql="select * from TB_PRODUCT where pro_id=?";
		try
		{
			connection=process.getConnection();
			PreparedStatement statement=connection.prepareStatement(sql);
			statement.setInt(1, id);
			ResultSet resultSet=statement.executeQuery();
			if(resultSet.next())
			{
				TB_PRODUCT product=new TB_PRODUCT();
				product.setPro_id(id);
				product.setPro_name(resultSet.getString("pro_name"));
				product.setPro_flg(resultSet.getString("pro_flg"));
				product.setChk_ymd(resultSet.getString("chk_ymd"));
				product.setChk_user_id(resultSet.getInt("chk_user_id"));
				product.setIn_ymd(resultSet.getString("in_ymd"));
				product.setIn_user_id(resultSet.getInt("in_user_id"));
				product.setPro_comnt(resultSet.getString("pro_comnt"));
				CLOB clob = ((OracleResultSet)resultSet).getCLOB("pro_inf");
				String pro_inf="";
				if(clob!=null)
				{
					Reader reader=clob.getCharacterStream();
					BufferedReader br=new BufferedReader(reader);
                    String s=null;
                    while((s=br.readLine())!=null)
                    	pro_inf+=s;
				}
				product.setPro_inf(pro_inf);
				product.setUpd_user_id(resultSet.getInt("upd_user_id"));
				product.setUpd_ymdhms(resultSet.getTimestamp("upd_ymdhms"));
				statement.close();
				return product;
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				process.closeConnection(connection);
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		return null;
	}

	public boolean removes(int[] list)
	{
		TB_PRODUCT product[]=new TB_PRODUCT[list.length];
		for(int i=0;i<product.length;i++)
		{	
			product[i]=new TB_PRODUCT();
			product[i].setPro_id(list[i]);
		}
		return DBUtil.remove(product);
	}

	@SuppressWarnings("deprecation")
	public int update(TB_PRODUCT product)
	{
		DBProcess process=new DBWrapper();
		Connection connection=null;
		String sql="update TB_PRODUCT set pro_name=?,pro_flg=?,chk_ymd=?,chk_user_id=?,in_ymd=?,in_user_id=?,pro_comnt=?,pro_inf=empty_clob(),upd_user_id=?,upd_ymdhms=? where pro_id="+product.getPro_id();
		try
		{
			connection=process.getConnection();
			connection.setAutoCommit(false);
			PreparedStatement s=connection.prepareStatement(sql);
			s.setString(2,product.getPro_name());
			s.setString(3, product.getPro_flg());
			s.setString(4,product.getChk_ymd());
			s.setBigDecimal(5, product.getChk_user_id());
			s.setString(6,product.getIn_ymd());
			s.setBigDecimal(7, product.getIn_user_id());
			s.setString(8, product.getPro_comnt());
			s.setBigDecimal(10,product.getUpd_user_id());
			s.setTimestamp(11,product.getUpd_ymdhms().timestampValue());
			int records=s.executeUpdate();
			String querySql="select pro_inf from TB_PRODUCT where pro_id="+product.getPro_id()+" for update";
            s=connection.prepareStatement(querySql);
            ResultSet rs=s.executeQuery();
            if(rs.next())
            {
            	CLOB clob = ((OracleResultSet)rs).getCLOB(1);
            	clob.putString(1,product.getPro_inf());
            	String ssql="update TB_PRODUCT set pro_inf=? where pro_id="+product.getPro_id();
            	s=connection.prepareStatement(ssql);
            	s.setClob(1, clob);
            	s.executeUpdate();
            }
            connection.commit();
            s.close();
            return records;
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		return 0;
	}

	public boolean removeAll()
	{
		return DBUtil.removeAll(new TB_PRODUCT());
	}

}

⌨️ 快捷键说明

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