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

📄 dyfiletable.java

📁 java 数据库连接通用源码 java 数据库连接通用源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		else strsql = strsql.substring(0,strsql.lastIndexOf("where"));
	}
	strsql+=" for update ";
	return strsql;
}
/**更新单条记录的大文件字段的sql语句 
*使用时可以将结果打印出来进行检查(建议一定要检查)
*<p>用于插入和更新文件字段
*@param boolean bflag true 带有rowid 的查询语句 false 一般查询语句
*@return String 经过组合的SQL语句
*@update 2001/8/7
*/
protected String SQLUpdateBlob(String rowid)throws DySQLException{
	if(blobset<0)throw new DySQLException("blob column set error: have not set the blob column.");
	String strsql = " , rowid ";
	String str = colName[blobset];//如果返回“*” 就不能加" rowid, "了
	strsql = " update "+strTbName+" SET "+str+" = ? where ";
	int iflag =0;
	if(rowid!=null&& !rowid.equals(""))strsql+= " rowid ='"+rowid+"'";
	else{
		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();
	try{
	operate(con,strsql,true,null);
	}catch(Exception e){
		e.printStackTrace();
		throw new SQLException(e.toString());
	}
	return 0;
}
/**单表更新
*数据插入表中
*@param Connection 
*@param String rowId   oracle数据库中的伪列的值,可以唯一标示一条记录。注意:该值应从数据库中查询出来
*@return int 0成功 其它 错误
*/
public int update(Connection con ,String rowId) throws SQLException {
	String strsql = SQLupdate(rowId);
	try{
	operate(con,strsql,true,rowId);
	}catch(Exception e){
		e.printStackTrace();
		throw new SQLException(e.toString());
	}
	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);
	try{
	operate(con,strsql,false,null);
	}catch(Exception e){
		e.printStackTrace();
		throw new SQLException(e.toString());
	}
	return 0;
}
/**单表删除
*数据插入表中
*@param Connection 
*@return int 0成功 其它 错误
*/
public int del(Connection con) throws SQLException{
	return del(con,null);
}
/**数据库操作
*@param Connection 
*@param String  
*@param boolean 是否对大文件字段的操作
*@param String rowid 
*/
private void operate(Connection con,String strsql,boolean bflag,String rowId)throws SQLException,DySQLException,DyIOException,IOException{
	try{
	Statement st = con.createStatement();
	st.execute(strsql);
	st.clearBatch();
}catch(SQLException e){
	 	throw new SQLException(e+"\n sql ="+strsql+" \n");
	}
		if(bflag){
			if(fin==null||fin.available()<=0)throw new DyIOException(" the file data is unavailable ");
			String strsqlx = SQLUpdateBlob(rowId);
			if(con instanceof OracleConnection){
				strsqlx = OracleSQLUpdateBlob(rowId);
				Statement st2 = con.createStatement();
				//System.out.println("sql2::"+strsqlx);
				ResultSet rs = st2.executeQuery(strsqlx);
				if(rs.next()){
        				oracle.sql.BLOB blob = ((OracleResultSet)rs).getBLOB(colName[blobset]);
        				OutputStream fout = blob.getBinaryOutputStream();
        				byte[] buffer = new byte[blob.getBufferSize()];

        				int bytesRead = 0;
        				while((bytesRead = fin.read(buffer)) != -1) {
           				   fout.write(buffer, 0, bytesRead);
              				}
              				fout.flush(); 
				        blob = null;
					buffer = null;
				        st2.close();
				        }//end if(rs.next())
			}//end if(con instanceof OracleConnection)
			else{
				PreparedStatement pstmt =con.prepareStatement(strsqlx);
				pstmt.setBinaryStream(1, fin, fin.available());
				pstmt.executeUpdate();
			        fin.close();
			        pstmt.close();
			}//end else(con instanceof OracleConnection)
			
		}//end if(bflag)
	
	
}
public InputStream searchFile(Connection ds)throws SQLException,DySQLException{
	return searchFile(ds,null);
}
/* destory at ver 0.91
public InputStream searchFile(DYDataSource ds,String rowid)throws SQLException,DySQLException{
	Connection con = ds.getConnection();
	searchFile(con,rowid);
	con.close();
	return fin;
}*/
public InputStream searchFile(Connection con,String rowid)throws SQLException,DySQLException{
	Statement st = con.createStatement();
	String strsql = SQLsearchBlob(rowid);
	Blob blob =null;
	ResultSet rs = st.executeQuery(strsql);
	if(rs.next()) {
				blob = rs.getBlob(1);				
			}
	else throw new DySQLException(" no result selected, check your select condition ");
	fin = new BufferedInputStream(blob.getBinaryStream());
	blob=null;
	st.close();
	return fin;
	}

/**单表查询
*结果值的列顺序 同 构建表结构时的列顺序
*<p>但不包括大文件字段,查询得到大文件字段见searchFile()
*@see searchFile(DYDataSource ds,String rowid)
*@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()throws DySQLException{
	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   throw new  DySQLException(" primary key column values set error: values not full, need more primary key value ");//任何主键的值不全,都不能进行相关操作
	}
	sPrimaryKey = sPrimaryKey.substring(0,sPrimaryKey.lastIndexOf("and"));
	return sPrimaryKey;
}
/**得到查询记录的所有列名(除大文件字段以外)
*@return String 成功 返回列名串(对日期型列名进行格式化) 
*
*/
private String getSearchCols()throws DySQLException{
	if(colName==null||colName.length==0) throw new DySQLException(" column names is null ,  check whether set column names ");
	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 if(i!=blobset)stemp+=" "+colName[i]+" ,";
			flag++;
		}//end formor else
	}
	if(flag==0) throw new DySQLException(" no column name selected, check whether set column names ");
	else{
		stemp = stemp.substring(0,stemp.lastIndexOf(","));
		return stemp;
		}
	}
}

⌨️ 快捷键说明

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