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

📄 update.java

📁 我自己实现了一个数据访问层
💻 JAVA
字号:
package dark.db.record;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Iterator;

/**
 * <p>Title:            数据库更新操作封装类</p>
 * <p>Description:      定义了对数据库记录的添加、更新和删除方法</p>
 * <p>Copyright:        Copyright (c) 2003</p>
 * <p>Company:          DIS</p>
 * <p>Create Time:      2004-3-21 12:25:30</p>
 * @author             <a href="mailto:dark_he@hotmail.com">darkhe</a>
 * @version            1.0
 */
public class Update
{	
	private String table_name;
	private String condition;	
	private Connection conn;

	private Record record = new Record();
	
		
	public Update(String table_name, Connection conn)
	{
		this.table_name = table_name;
		this.conn = conn;	
	}
	
	
	public Update()
	{
	}
	
	/**
	 * 添加记录
	 * @throws SQLException
	 */
	public void doInsert() throws SQLException
	{
		// 得到Insert串值
		String sql = getInsertSQL();
		
		// 执行实际的更新操作
		executeUpdate( sql );
	}
	

	/**
	 * 更新记录
	 * @throws SQLException
	 */
	public void doUpdate() throws SQLException
	{
		// 得到update串值
		String sql = getUpdateSQL();
		
		// 执行实际的更新操作
		executeUpdate(sql);
	}
	
	
	/**
	 * 删除记录
	 * @throws SQLException
	 */
	public void doDelete() throws SQLException
	{
		// 得到del串值
		String sql = getDeleteSQL();
		
		// 执行实际的更新操作
		executeUpdate(sql);
	}
		

	/**
	 * 根据转入sql串值执行数据库更新操作
	 * @param sql
	 */
	public void executeUpdate(String sql) throws SQLException
	{		
		System.out.println( this + ">>>SQL>>>" + sql );
				
		if( conn == null )
		{
			throw new SQLException("没有指定数据库连接");
		}
		
		Statement st = conn.createStatement();
		st.executeUpdate( sql );
		st.close();						
	}
	
			
	/**
	 * @return 构造的Delete SQL串值
	 */
	public String getDeleteSQL() throws SQLException
	{
		StringBuffer sql = new StringBuffer();
		
		sql.append("DELETE FROM ");		
		     
		if( table_name == null )
		{
			throw new SQLException("未设置表名" );
		}
		else
		{
			sql.append( table_name );
		}
            
		if( condition != null )    
		{
			sql.append(" WHERE ");
			sql.append( condition );
		}
      
		return sql.toString();
	}

	
			
	/**
	 * @return 构造的Update SQL串值
	 */
	public String getUpdateSQL() throws SQLException
	{
		StringBuffer sql = new StringBuffer();

		sql.append("UPDATE ");
		
		if( table_name == null )
		{
			throw new SQLException("未设置表名" );
		}
		else
		{
			sql.append( table_name );
		}
        
		sql.append(" SET ");      
    
		if( isEmpty() )
		{
			throw new SQLException("未设置字段信息" );
		}    
        
		// 遍历组合字段信息
		Iterator itKey = record.keySet().iterator();
		StringBuffer fv = new StringBuffer();		
		while( itKey.hasNext() )
		{			
			String field = (String)itKey.next();
			fv.append( field );
			fv.append("=");
												
			Object obj = record.get( field );			
			if( obj instanceof Byte )					
			{				
				fv.append( ((Byte)obj).byteValue() );
			}
			else if( obj instanceof Short )					
			{				
				fv.append( ((Short)obj).shortValue() );
			}
			else if( obj instanceof Integer )					
			{				
				fv.append( ((Integer)obj).intValue() );
			}
			else if( obj instanceof Long )					
			{				
				fv.append( ((Long)obj).longValue() );
			}
			else if( obj instanceof Float )					
			{				
				fv.append( ((Float)obj).floatValue() );
			}
			else if( obj instanceof Double )					
			{				
				fv.append( ((Double)obj).doubleValue() );
			}
			else if( obj instanceof Seq )
			{
				fv.append( ((Seq)obj).toString() );
			}			
			else if( obj instanceof String )
			{
				fv.append("'");
				fv.append( (String)obj );
				fv.append("'");
			}			
			
			fv.append(",");
		}    	
        
		// 去除多余的","        
		String fvlist = fv.substring(0, fv.length()-1 );
        
		sql.append( fvlist );
        
        
		if( condition != null)
		{
		  sql.append(" WHERE ");
		  sql.append( condition );
		}
		
		return sql.toString();
	}



	/**
	 * @return 构造的Insert SQL串值
	 */
	public String getInsertSQL() throws SQLException
	{
		StringBuffer sql = new StringBuffer();
		
		sql.append("INSERT INTO ");

		if( table_name == null )
		{			
			throw new SQLException("未设置表名");			
		}
		else
		{
			sql.append( table_name );
		}


		if( isEmpty() )
		{			
			throw new SQLException("未设置字段信息");		
		}
		else
		{
			sql.append("(");
			
			// 遍历key来组合字段名列表
			Iterator itKey = record.keySet().iterator();
			StringBuffer field = new StringBuffer();
			while( itKey.hasNext() )
			{
				field.append( (String)itKey.next() );
				field.append(",");
			}
			
			// 去掉多余的","
			String fieldlist = field.substring(0, field.length()-1 );
						
			sql.append( fieldlist );		 	
			sql.append(") ");
		}

		sql.append("VALUES (");		
		
		// 遍历value来组合字段值列表
		Iterator itValue = record.values().iterator();
		StringBuffer value = new StringBuffer();
		while( itValue.hasNext() )
		{			
			Object obj = itValue.next();
			if( obj instanceof Byte )					
			{				
				value.append( ((Byte)obj).byteValue() );
			}
			else if( obj instanceof Short )					
			{				
				value.append( ((Short)obj).shortValue() );
			}
			else if( obj instanceof Integer )					
			{				
				value.append( ((Integer)obj).intValue() );
			}
			else if( obj instanceof Long )					
			{				
				value.append( ((Long)obj).longValue() );
			}
			else if( obj instanceof Float )					
			{				
				value.append( ((Float)obj).floatValue() );
			}
			else if( obj instanceof Double )					
			{				
				value.append( ((Double)obj).doubleValue() );
			}		
			else if( obj instanceof Seq )
			{
				value.append( ((Seq)obj).toString() );
			}
			else if( obj instanceof String )
			{
				value.append("'");
				value.append( (String)obj );
				value.append("'");
			}
			
			value.append(",");
		}
		
		// 去掉最后一个多余的","
		String valuelist = value.substring(0, value.length() -1 );
													
		sql.append( valuelist );
		
		sql.append(")");
		
		return sql.toString();																			
	}


	/**
	 * 清除所有字段信息
	 */
	public void clear()
	{
		record.clear();
	}

	/**
	 * @param key
	 * @return 字段名称所对应的字段值
	 */
	public Object get(Object key)
	{
		return record.get(key);
	}

	/**
	 * @return 如果字段列表为空则返回真,否则返回假
	 */
	public boolean isEmpty()
	{
		return record.isEmpty();
	}

	/**
	 * @param key
	 * @param value
	 * @return
	 */
	public Object put(Object key, Object value)
	{
		return record.put(key, value);
	}

	/**
	 * @return 当前字段个数
	 */
	public int size()
	{
		return record.size();
	}
	
	
	/**
	 * 测试方法
	 * @param args
	 * @throws SQLException
	 */
	public static void main(String[] args)
	{
		Connection conn = null;
		Update r = new Update("TableName", conn );
		r.put("ID", new Seq("seq_gwid.nextval"));
		r.put("Name", "darkhe");
		r.put("Date", new Date().toString() );
		r.put("SEX",  new Float(0) );
		
		//r.setCondition( "id=100" );
		
		try
		{
			// 测试getInsertSQL()
			System.out.println(" insert= " + r.getInsertSQL() );
			// 测试getUpdateSQL()
			System.out.println(" update= " + r.getUpdateSQL() );
			// 测试getDeleteSQL()
			System.out.println(" delete= " + r.getDeleteSQL() );
			
			//r.insert();	
		}
		catch (SQLException e)
		{
			System.out.print( e );
			e.printStackTrace();
		}	
	}


	/**
	 * @return
	 */
	public String getCondition()
	{
		return condition;
	}

	/**
	 * @param string
	 */
	public void setCondition(String string)
	{
		condition = string;
	}

	/**
	 * @return
	 */
	public Connection getConnection()
	{
		return conn;
	}

	/**
	 * @return
	 */
	public Record getRecord()
	{
		return record;
	}

	/**
	 * @return
	 */
	public String getTableName()
	{
		return table_name;
	}

	/**
	 * @param connection
	 */
	public void setConnection(Connection connection)
	{
		conn = connection;
	}

	/**
	 * @param map
	 */
	public void setRecord(Record record)
	{
		this.record = record;
	}

	/**
	 * @param string
	 */
	public void setTableName(String string)
	{
		table_name = string;
	}

}

⌨️ 快捷键说明

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