【Java开源代码栏目提醒】:网学会员--在 Java开源代码编辑为广大网友搜集整理了:CommonBean.java绩等信息,祝愿广大网友取得需要的信息,参考学习。
package com.db;
import java.lang.*;
import java.util.*;
import java.sql.*;
import com.db.ParentBean;
/**
* 此类继承ParentBean,用于作为所有单表类的父类,如果没有复杂操作,则只用此类即可
* 使用此类要求数据表主键为ID
* 表名和字段名最好用小写
*/
public class CommonBean extends ParentBean
{
public String id = "-1";
private String tabName = "";
private String orderBy = ""; //排序字段:"f1,f2"
private String tabInfo = ""; //增加修改时用表格信息:"f1#type1#only1#f2#type2#only2"
//如果only1为1,则表示该字段为唯一字段之一
public void setID(String id) { this.id = id; }
protected void setTabName(String tabName) { this.tabName = tabName; }
protected void setOrderBy(String orderBy) { this.orderBy = orderBy; }
protected void setTabInfo(String tabInfo) { this.tabInfo = tabInfo; }
protected void init(String n,String b,String i) { setTabName(n); setOrderBy(b); setTabInfo(i); }
/**取得当前页信息*/
public Vector getCurPage(int cur,int records)
{
return getOnePageByMSSQL("select * from " + tabName + " order by " + orderBy , cur, records);
}
/**取得一行信息*/
public Hashtable getOneData()
{
Hashtable hash = new Hashtable ( ) ;
String sql = " select * from " + tabName + " where id= " + id + " order by " + orderBy;
Vector vect = ( Vector ) getDataBySql ( sql ) ;
if ( vect.size ( ) > 0 )
{
hash = ( Hashtable ) vect.get ( 0 ) ;
}
return hash ;
}
public Vector getAllData( String wherestr )
{
Hashtable hash = new Hashtable ( ) ;
String sql = " select * from " + tabName + " order by " + orderBy;
if ( !wherestr.equals("") )
sql = " select * from " + tabName + " where " + wherestr + " order by " + orderBy;
return getDataBySql ( sql ) ;
}
/**增加记录*/
public int add(Hashtable hash)
{
Vector vect =new Vector();
vect.add(tabName);
if ( (String)hash.get("id") != null )
{
vect.add(addVector("id",(String)hash.get("id"),"NUM"));
}
else
{
vect.add(addVector("id",""+makeID(tabName,"id","","",true),"NUM"));
}
//提取所有字段信息
String wheres = "";
String str[] = ds.splitStr(tabInfo,'#');
for (int i = 0 ; i < str.length ; )
{
String value = (String) hash.get( str[i] ) ;
if ( value!=null )
{
value = ds.toString ( value ) ;
//形成唯一字段判断条件
if( str[i+2].equals("1"))
{
if( !wheres.equals("") ) wheres += " and ";
if( str[i+1].equals("CHAR") ) wheres += str[i] + "='" + value + "'";
else if( str[i+1].equals("NUM") ) wheres += str[i] + "=" +value;
else if( str[i+1].equals("TIME") ) wheres += str[i] + "=to_date('yyyy-MM-dd HH:mm:ss','" + value + "')";
}
if ( str[i].equals( "id" ) )
{
i += 3 ;
continue ;
}
vect.add( addVector( str[i] , value , str[i+1] ) );
}
i += 3 ;
}
String sql = "select * from " + tabName;
if( !wheres.equals("") )
{
sql += " where " + wheres;
if(getDataBySql(sql).size()!=0)
{
return 1;
}
}
return insertRecord(vect);
}
/**修改记录*/
public int mod(Hashtable hash)
{
Vector vect =new Vector();
vect.add(tabName);
//提取所有字段信息
String wheres = "";
String str[] = ds.splitStr(tabInfo,'#');
for (int i = 0 ; i < str.length ; )
{
String value = (String) hash.get( str[i] ) ;
if ( value != null )
{
value = ds.toString ( value ) ;
//形成唯一字段判断条件
if( str[i+2].equals("1"))
{
if( !wheres.equals("") ) wheres += " and ";
if( str[i+1].equals("CHAR") ) wheres += str[i] + "='" + value + "'";
else if( str[i+1].equals("NUM") ) wheres += str[i] + "=" +value;
else if( str[i+1].equals("TIME") ) wheres += str[i] + "=to_date('yyyy-MM-dd HH:mm:ss','" + value + "')";
}
vect.add( addVector( str[i] , value , str[i+1] ) );
}
i += 3;
}
if ( !id.equals( "-1" ) )
{
vect.add("id="+id+"");
}
String sql = "select * from " + tabName + " where id!=" + id;
if( !wheres.equals(""))
{
sql += " and " + wheres;
if(getDataBySql(sql).size()!=0)
{
return 1;
}
}
return updateRecord(vect);
}
/**删除记录*/
public int del()
{
return deleteRecord("delete from " + tabName + " where id="+id);
}
public static void main(String args[])
{
CommonBean mb = new CommonBean();
//初始化表格信息
mb.init("map","x,y","x#CHAR#0#y#CHAR#0");
//增加记录
Hashtable h = new Hashtable();
h.put("x","111");
h.put("y","111");
//mb.add(h);
mb.setID("10");
System.out.println(mb.mod(h));
System.out.println(mb.getCurPage(1,10));
mb.closeConn();
}
};
上一篇:
CommonaJdbc.java
下一篇:
ASP小区停车管理系统(Access)(含录像)