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

📄 dytable.java

📁 java 数据库连接通用源码 java 数据库连接通用源码
💻 JAVA
字号:
package com.dyit.sql;

import java.util.*;
import java.sql.*;
 
/* 
* DYtable  	ver 1.01   beginner 0.9
* author :zhanghan             
* write-time :2001.4.16
* debug-time: 2001.4.27
* write for dyit-bsjs com
* function :  abstract table
*
* debug:0.91  packaged sql run method ,add 'lColumn' field to specify Column
*	0.92  change back no lColumn , packaged methods to sub table class to set column value
*		change primary key to LinkedList;
*       0.93  update insert sql on date format;change date form as 'yyyy-mm-dd hh24:mi:ss'
*	0.94	change to abstract class, its subclass instant physical db table.
*	0.95   debug SQLsearch()
*	0.96   update SQLsearch()
*	0.97   update all support rowid update,del ,search
*       0.98   debug setValue() sometime when reuse didn't clear the old one
*	1.01   change package 
*/
/**提供对单个数据库表的 插入,修改,删除,查询
*<p>所有列变量都以string类型传入, Date型变量 有两种写法
*<p>"YYYY-MM-DD" 或 "YYYY-MM-DD hh24:mi:ss" 注意空格
*<p>注意 :实际数据库表中的日期 字段都要以"D_"开头,否则无法自动生成SQL语句
*
*/
public abstract class DYtable {
/**列的个数
*/
protected int iTbColumn ;
/**表名
*/
protected String strTbName = "";
/**数据值 每对数据包括(列名,列值)
*/
protected HashMap hColumn = new HashMap();//数据值
/**主键组, 允许有多个主键,构建表类时应添加所有主键到 该组中
*/
protected LinkedList lPrmKey = new LinkedList();//主键组
protected String[] colName;
//protected String sPrimaryKey = "";

/**构建表结构,在类生成时调用
*包括 表名、列名组、主键(组)的设置
*/
public abstract void constructTable();
/**得到表的列的个数,列个数应在constructTable()中设置
*@return int 列的个数
*/
public int getTbCol(){return iTbColumn;}
/**设置全体列值
*@param String[] 列值 按构建顺序(如果库表中的列可以为空,该列可以缺项)
*@return int 0 设置成功,1失败
*/
public int setValues(java.lang.String[] Values){
	hColumn.clear();//update 2001-9-10 v0.98
	if(Values.length!=iTbColumn)return 1;
	for(int i=0;i<iTbColumn;i++)set(colName[i],Values[i]);
	return 0;
}
/**得到单条记录的插入语句
*使用时可以将结果打印出来进行检查
*@return String 经过组合的SQL语句
*/
public String SQLinsert(){
	//String strsql = "insert into "+strTbName+" values(";
	String strsql = " values(";//值
	String strsql1 = "(";//列
	Iterator iterCol = hColumn.keySet().iterator();
	while(iterCol.hasNext()){
		String stemp = (String)iterCol.next();
		String stempValue = (String)hColumn.get(stemp);
		if(stempValue!=null&&!stempValue.equals("")){
			strsql1 = strsql1+stemp+",";
			if(stemp.indexOf("D_")!=-1){
				//表中日期数据以D_ 开头,对其格式化一下
				if(stempValue.indexOf(":")>0)
					strsql = strsql+"to_date('"+stempValue+"','yyyy-mm-dd hh24:mi:ss'), ";
				else	strsql = strsql+"to_date('"+stempValue+"','yyyy-mm-dd '), ";
				}
			else strsql = strsql+"'"+stempValue+"' , ";
			}
		}
	strsql = "insert into "+strTbName+strsql1.substring(0,strsql1.lastIndexOf(","))+")"+strsql.substring(0,strsql.lastIndexOf(","))+")";
	return strsql;
	}
/**得到单条记录的修改语句
*使用时可以将结果打印出来进行检查
*@return String 经过组合的SQL语句
*/
public String SQLupdate(){
	return SQLupdate(null);
}
/**得到单条记录的修改语句
*使用时可以将结果打印出来进行检查
*@param String rowId   oracle数据库中的伪列的值,可以唯一标示一条记录。注意:该值应从数据库中查询出来
*@return String 经过组合的SQL语句
*@update 2001-8-29
*/
public String SQLupdate(String rowId){
	String sPrimaryKey = null;
	if(rowId==null||rowId.equals("")){
		sPrimaryKey = getPrmKeyCndtion();
		if(sPrimaryKey.equals("-1"))return "主键的值不全,不能进行相关操作";
	}
	else sPrimaryKey =" rowid ='"+rowId+"'";
	String strsql = "Update "+strTbName+" set ";
	Iterator iterCol = hColumn.keySet().iterator();
	while(iterCol.hasNext()){
		String stemp = (String)iterCol.next();
		String stempValue = (String)hColumn.get(stemp);
		if(stempValue!=null&&!stempValue.equals("")){
			if(stemp.indexOf("D_")!=-1){
				//表中日期数据以D_ 开头,对其格式化一下
				if(stempValue.indexOf(":")>0)
					strsql = strsql+stemp+"=to_date('"+stempValue+"','yyyy-mm-dd hh24:mi:ss'), ";
				else	strsql = strsql+stemp+"=to_date('"+stempValue+"','yyyy-mm-dd '), ";
				}
			else strsql = strsql+stemp+"='"+stempValue+"' , ";
			}
	}
	
	strsql = strsql.substring(0,strsql.lastIndexOf(","))+" where "+sPrimaryKey;
	
	return strsql;
}
/**得到单条记录的删除语句
*使用时可以将结果打印出来进行检查
*@return String 经过组合的SQL语句
*/
public String SQLdel(){
	return SQLdel(null);
	}
/**得到单条记录的删除语句
*使用时可以将结果打印出来进行检查
*@param String rowId   oracle数据库中的伪列的值,可以唯一标示一条记录。注意:该值应从数据库中查询出来
*@return String 经过组合的SQL语句
*@update 2001-8-29
*/
public String SQLdel(String rowId){
	String sPrimaryKey = null;
	if(rowId==null||rowId.equals("")){
		sPrimaryKey = getPrmKeyCndtion();
		if(sPrimaryKey.equals("-1"))return "主键的值不全,不能进行相关操作";
	}
	else sPrimaryKey =" rowid ='"+rowId+"'";
	String strsql = "delete from "+strTbName+" where "+sPrimaryKey;
	return strsql;
}
/**得到单条记录的查询语句
*使用时可以将结果打印出来进行检查
*@return String 经过组合的SQL语句
*@update 2001/8/7
*/
public String SQLsearch(){
	return SQLsearch(false);
	}
/**得到单条记录的查询语句
*使用时可以将结果打印出来进行检查(建议一定要检查)
*@param boolean bflag true 带有rowid 的查询语句 false 一般查询语句
*@return String 经过组合的SQL语句
*@update 2001/8/7
*/
public String SQLsearch(boolean bflag){
	String strsql = " , rowid ";
	String str = getSearchCols();//如果返回“*” 就不能加" rowid, "了
	if(bflag&&!str.equals("*"))strsql = "select "+str+strsql+" from "+strTbName+" where ";
	else strsql = "select "+str+" from "+strTbName+" where ";
	int iflag =0;
	Iterator iterCol = hColumn.keySet().iterator();
	while(iterCol.hasNext()){
		String stemp = (String )iterCol.next();
		String stempValue = (String)hColumn.get(stemp);
		if(stempValue!=null&&!stempValue.equals("")){
				if(stemp.indexOf("D_")!=-1){
				//表中日期数据以D_ 开头,对其格式化一下
					if(stempValue.indexOf(":")>0)
						strsql = strsql+stemp+"=to_date('"+stempValue+"','yyyy-mm-dd hh24:mi:ss') and ";
					else	strsql = strsql+stemp+"=to_date('"+stempValue+"','yyyy-mm-dd ') and ";
					}
				else strsql = strsql+stemp+"='"+stempValue+"' and ";
				iflag++;
			}
		}
	//hColumn有值 最后的strsql多余了 一个 "and" 
	if(iflag>0)strsql = strsql.substring(0,strsql.lastIndexOf("and"));
	//hColumn空值 最后的strsql多余 "where"
	else strsql = strsql.substring(0,strsql.lastIndexOf("where"));
	return strsql;
}
/**单表插入
*数据插入表中
*@param Connection 
*@return int 0成功 其它 错误
*/
public int insert(Connection con) throws SQLException{
	String strsql = SQLinsert();
	operate(con,strsql);
	return 0;
}
/**单表更新
*数据插入表中
*@param Connection 
*@param String rowId   oracle数据库中的伪列的值,可以唯一标示一条记录。注意:该值应从数据库中查询出来
*@return int 0成功 其它 错误
*/
public int update(Connection con ,String rowId) throws SQLException{
	String strsql = SQLupdate(rowId);
	operate(con,strsql);
	return 0;
}
/**单表更新
*数据插入表中
*@param Connection 
*@return int 0成功 其它 错误
*/
public int update(Connection con) throws SQLException{
	return update(con,null);
}
/**单表删除
*数据插入表中
*@param Connection 
*@param String rowId   oracle数据库中的伪列的值,可以唯一标示一条记录。注意:该值应从数据库中查询出来
*@return int 0成功 其它 错误
*/
public int del(Connection con,String rowId) throws SQLException{
	String strsql = SQLdel(rowId);
	operate(con,strsql);
	return 0;
}
/**单表删除
*数据插入表中
*@param Connection 
*@return int 0成功 其它 错误
*/
public int del(Connection con) throws SQLException{
	return del(con,null);
}
/**数据库操作
*@param Connection 
*@param String  
*/
private void operate(Connection con,String strsql)throws SQLException{
	Statement st = con.createStatement();
	st.execute(strsql);
	st.close();
}
/**单表查询
*结果值的列顺序 同 构建表结构时的列顺序
*@param DYDataSource
*@param boolean bflag true 带有rowid 的查询语句 false 一般查询语句
*@return DYquery
*/
public DYquery search(DYDataSource ds,boolean bflag){
	DYquery dq = new DYquery();
	//System.out.println(SQLsearch(bflag));
	dq.executeSql(ds,SQLsearch(bflag));
 	return dq;
}
/**单表查询
*结果值的列顺序 同 构建表结构时的列顺序
*@param DYDataSource 
*@return DYquery
*/
public DYquery search(DYDataSource ds){
 	return search(ds,false);
}
/**设置单列值
*@param String column 列名
*@param String value  列值
*/
protected void set(String column, String value){
		hColumn.put(column,value);
}
/**增加主键(列名)
*@param String 作为主键的列名
*/
protected void setPrmKey(String spk){
	lPrmKey.add(spk);
	}
/**得到单条记录的主键条件,确定要操作的记录
*@return String 成功 返回主键条件 失败 返回"-1" 表示"主键的值不全,不能进行相关操作".
*
*/
private String getPrmKeyCndtion(){
	String sPrimaryKey ="";
	for(int i=0;i<lPrmKey.size();i++){
		String stemp = (String)lPrmKey.get(i);
		String stempValue = (String)hColumn.get(stemp);
		if(stempValue!=null&&!stempValue.equals("")){
			if(stemp.indexOf("D_")!=-1){
				//表中日期数据以D_ 开头,对其格式化一下
				if(stempValue.indexOf(":")>0)
					sPrimaryKey = sPrimaryKey+stemp+"=to_date('"+stempValue+"','yyyy-mm-dd hh24:mi:ss') and ";
				else	sPrimaryKey = sPrimaryKey+stemp+"=to_date('"+stempValue+"','yyyy-mm-dd ') and ";
				}
			else sPrimaryKey = sPrimaryKey+stemp+"='"+stempValue+"' and ";
			}
		else   return "-1";//任何主键的值不全,都不能进行相关操作
	}
	sPrimaryKey = sPrimaryKey.substring(0,sPrimaryKey.lastIndexOf("and"));
	return sPrimaryKey;
}
/**得到查询记录的所有列名
*@return String 成功 返回列名串(对日期型列名进行格式化)<p>失败 返回"*" 依然可以查询.但日期型数据返回"YYYY-MM-DD hh24:mi:ss.s"
*
*/
private String getSearchCols(){
	if(colName==null||colName.length==0)return "*";
	int flag =0;
	String stemp="";
	for(int i=0;i<colName.length;i++){
		if(colName[i]==null||colName[i].equals(""))continue;//没有列名 下一循环
		else {if(colName[i].indexOf("D_")!=-1){
				//表中日期数据以D_ 开头,对其格式化一下
				stemp+=" to_char( "+colName[i]+",'yyyy-mm-dd hh24:mi:ss') ,";
				}
			else	stemp+=" "+colName[i]+" ,";
			flag++;
		}//end formor else
	}
	if(flag==0) return "*";
	else{
		stemp = stemp.substring(0,stemp.lastIndexOf(","));
		return stemp;
		}
	}
}

⌨️ 快捷键说明

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