我用arraylist,代码太长,不好贴
解决方案 »
- 现有一个32 位的整型变量 value 和一个有32 个元素的数组a[32]
- hibernate 自关联 多对多关系
- java处理大量字符串
- txt 导出时数据时,数据不一致 请问高手什么问题(急)
- Struts的开发环境怎么搭建~~~来者有分~~~
- 跪求JSP错误该的解决方法
- rmi 连接sql server2000数据库报错 classNotFound 和no suitable driver 的错误
- 关于ejb打包时候的问题,路径设置
- 试试运气,有没有人碰到这样子的错误?折腾了一天了,,,,
- struts的问题
- 为什么运行rmi客户端也需要指定安全policy文件???
- (又是100分)Keyword: tomcat、jndi、ldap (up有理!)
remanwang(玩玩儿) 帮忙啊
在把对象放进arraylist
2.Vector的内存开销比array要大得多。
{
protected Integer _ID ;
protected Integer _DOCID ;
protected String _FILENAME ;
protected String _REALNAME ;
public Integer get_ID()
{
return this._ID;
}
public void set_ID(Integer a_strID)
{
this._ID=a_strID;
}
public Integer get_DOCID()
{
return this._DOCID;
}
public void set_DOCID(Integer a_strDOCID)
{
this._DOCID=a_strDOCID;
}
public String get_FILENAME()
{
return this._FILENAME;
}
public void set_FILENAME(String a_strFILENAME)
{
this._FILENAME=a_strFILENAME;
}
public String get_REALNAME()
{
return this._REALNAME;
}
public void set_REALNAME(String a_strREALNAME)
{
this._REALNAME=a_strREALNAME;
}
/****************************************************************
* 构造函数
****************************************************************/
public DocAttch()
{ this._ID= null;
this._DOCID= null;
this._FILENAME= null;
this._REALNAME= null;
}
}
{ public DocAttchImpl()
{
} public void settxtid(String s)
{
Integer si;
if(s != null)
{
si=new Integer(s);
this.set_ID(si);
}
}
public void settxtdocid(String s)
{
Integer si;
if(s != null)
{
si=new Integer(s);
this.set_DOCID(si);
}
}
public void settxtfilename(String s)
{
if(s != null)
this.set_FILENAME(s);
}
public void settxtrealname(String s)
{
if(s != null)
this.set_REALNAME(s);
} public boolean Create() throws Exception
{
int i=0;
java.sql.Connection myConn = null;
java.sql.Statement st = null; String strSql = "insert into [doc_attch]([DOCID],[FILENAME],[REALNAME]) values(";
if(this.get_DOCID()!=null) strSql=strSql + this.get_DOCID().toString() + ",";
else strSql=strSql + "null" + ",";
if(this.get_FILENAME()!=null) strSql=strSql + "'" + this.ToSql(this.get_FILENAME()) + "'" + ",";
else strSql=strSql + "null" + ",";
if(this.get_REALNAME()!=null) strSql=strSql + "'" + this.ToSql(this.get_REALNAME()) + "'" + ")";
else strSql=strSql + "null" + ")"; try
{
myConn = DbConnectionManager.getConnection() ;
st = myConn.createStatement() ;
i=st.executeUpdate(strSql);
}
catch(Exception e)
{
}
finally
{
if(st!=null) st.close();
if(myConn!=null) myConn.close();
}
if(i>0){return true;}else{return false;}
}
public boolean Delete() throws Exception
{
java.sql.Connection myConn = null;
java.sql.Statement st = null;
int i=0; String strSql = "delete from [doc_attch] where [id]=" + this.get_ID().toString() ; cat.info(strSql);
try
{
myConn = DbConnectionManager.getConnection() ;
st = myConn.createStatement() ;
i=st.executeUpdate(strSql);
}
catch(Exception e)
{
i=-1;
}
finally
{
if(st!=null) st.close();
if(myConn!=null) myConn.close();
}
if(i>=0){return true;}else{return false;}
} public boolean Save() throws Exception
{
java.sql.Connection myConn = null;
java.sql.Statement st = null;
int i=0; String strSql = "update [doc_attch] set ";
strSql=strSql + "docid=";
if(this.get_DOCID()!=null) strSql=strSql + this.get_DOCID().toString() + ",";
else strSql=strSql + "null" + ",";
strSql=strSql + "filename=";
if(this.get_FILENAME()!=null) strSql=strSql + "'" + this.ToSql(this.get_FILENAME()) + "'" + ",";
else strSql=strSql + "null" + ",";
strSql=strSql + "realname=";
if(this.get_REALNAME()!=null) strSql=strSql + "'" + this.ToSql(this.get_REALNAME()) + "'";
else strSql=strSql + "null";
strSql = strSql + " where [id]= " + this.get_ID().toString() ;
try
{
myConn = DbConnectionManager.getConnection() ;
st = myConn.createStatement() ;
i=st.executeUpdate(strSql);
}
catch(Exception e)
{
}
finally
{
if(st!=null) st.close();
if(myConn!=null) myConn.close();
}
if(i>0){return true;}else{return false;}
}
{
java.sql.Connection myConn = null;
java.sql.Statement st = null;
java.sql.ResultSet rs = null; int i=0;
boolean flag=false;
int itmp;
double dtmp;
String stmp;
java.util.Date rtmp;
String strSql = "select * from [doc_attch] where [id]=";
strSql=strSql + Integer.toString(a_intid);
try
{
myConn = DbConnectionManager.getConnection() ;
st = myConn.createStatement();
rs = st.executeQuery(strSql) ;
if(rs.next())
{ itmp=rs.getInt("ID");
if(!rs.wasNull()) this._ID=new Integer(rs.getInt("ID"));
itmp=rs.getInt("DOCID");
if(!rs.wasNull()) this._DOCID=new Integer(rs.getInt("DOCID"));
stmp=rs.getString("FILENAME");
if(!rs.wasNull()) this._FILENAME=theTrim(rs.getString("FILENAME"));
stmp=rs.getString("REALNAME");
if(!rs.wasNull()) this._REALNAME=theTrim(rs.getString("REALNAME"));
flag=true;
}
}
catch(Exception e)
{
}
finally
{
rs=null;
if(st!=null) st.close();
if(myConn!=null) myConn.close();
}
return flag ;
} public ArrayList LoadByPage(int PageNumber,int PageSize,String Condition) throws Exception
{
java.sql.Connection myConn = null;
java.sql.Statement st = null;
java.sql.ResultSet rs = null;
ArrayList al=new ArrayList();
int i;
int itmp;
double dtmp;
String stmp;
java.util.Date rtmp;
String strSql = "select id,doc_no,script_no,title,createdate from [doc_attch] "; try
{
myConn = DbConnectionManager.getConnection() ;
st = myConn.createStatement(1004,1007);
if(!Condition.equals(""))
{
Condition=" where " + Condition;
}
rs = st.executeQuery(strSql + Condition) ;
rs.absolute((PageNumber-1)*PageSize+1);
i=0;
while(i<PageSize && !rs.isAfterLast())
{
DocAttch fc=new DocAttch(); itmp=rs.getInt("ID");
if(!rs.wasNull()) fc.set_ID(new Integer(rs.getInt("ID")));
al.add(fc);
rs.next();
i++;
}
}
catch(Exception e)
{
al=null;
}
finally
{
rs=null;
if(st!=null) st.close();
if(myConn!=null) myConn.close();
}
return al ;
} public ArrayList LoadAll(String Condition) throws Exception
{
java.sql.Connection myConn = null;
ArrayList al=new ArrayList();
try
{
myConn = DbConnectionManager.getConnection() ;
al=LoadAll(Condition,myConn);
}
catch(Exception e)
{
al=null;
}
finally
{
if(myConn!=null) myConn.close();
}
return al ;
} public ArrayList LoadAll(String Condition,java.sql.Connection myConn) throws Exception
{
java.sql.Statement st = null;
java.sql.ResultSet rs = null;
ArrayList al=new ArrayList();
int i;
int itmp;
double dtmp;
String stmp;
java.util.Date rtmp;
String strSql = "select * from [doc_attch]"; try
{
st = myConn.createStatement(1004,1007);
if(!Condition.equals(""))
{
Condition=" where " + Condition;
}
rs = st.executeQuery(strSql + Condition) ;
i=0;
while(!rs.isAfterLast())
{
DocAttch fc=new DocAttch(); itmp=rs.getInt("ID");
if(!rs.wasNull()) fc.set_ID(new Integer(rs.getInt("ID")));
itmp=rs.getInt("DOCID");
if(!rs.wasNull()) fc.set_DOCID(new Integer(rs.getInt("DOCID")));
stmp=rs.getString("FILENAME");
if(!rs.wasNull()) fc.set_FILENAME(theTrim(rs.getString("FILENAME")));
stmp=rs.getString("REALNAME");
if(!rs.wasNull()) fc.set_REALNAME(theTrim(rs.getString("REALNAME")));
al.add(fc);
rs.next();
}
}
catch(Exception e)
{
al=null;
}
finally
{
rs=null;
if(st!=null) st.close();
}
return al ;
} public ArrayList LoadAll(String a_strFieldName,int a_intFID) throws Exception
{
Integer itmp=new Integer(a_intFID);
String strSql=a_strFieldName + "=" + itmp.toString();
return LoadAll(strSql) ;
}
public int Count() throws Exception
{
java.sql.Connection myConn = null;
java.sql.Statement st = null;
java.sql.ResultSet rs = null;
int fc=0; try
{
myConn = DbConnectionManager.getConnection() ;
st = myConn.createStatement() ;
rs = st.executeQuery("select count(*) as doc_attchCount from [doc_attch] ");
if(rs.next())
{
fc=rs.getInt("doc_attchCount");
}
}
catch(Exception e)
{
fc=-1;
}
finally
{
rs=null;
if(st!=null) st.close();
if(myConn!=null) myConn.close();
}
return fc ;
} private String ToSql(String a_strSql)
{
return StringUtils.replace(a_strSql, "'", "''");
}
private String theTrim(String a_strSql)
{
return a_strSql.trim();
}}
//FilesBean.java
....
public Enumeration ejbFindByTitle(String title,int order) throws FinderException,RemoteException{
//通用数据库连接
PreparedStatement pstmt=null;
Connection conn=null;
DataSource ds = null;
Vector v=new Vector();
try{
//System.out.println("ejbFindByTitle("+title+")方法被调用。");
//System.out.println("begin to connect the database...");
/*
* 获得数据库连接
*/
Context ic = new InitialContext();
ds = (DataSource)ic.lookup("jdbc/webbos");
conn = ds.getConnection();
/*
* 在数据库中查找这个标题
*/
//System.out.println("begin scaning...");
if(order==EInclude.ORDERBYNULL)
pstmt=conn.prepareStatement("select fileID from file where title like ?");
if(order==EInclude.ORDERBYAUTHOR)//order by author
pstmt=conn.prepareStatement("select fileID from file where title like ? order by author");
if(order==EInclude.ORDERBYUPLOADDATE)//order by uploadDate;
pstmt=conn.prepareStatement("select fileID from file where title like ? order by uploadDate");
if(order==EInclude.ORDERBYSUBJECTID)//order by subjectID
pstmt=conn.prepareStatement("select fileID from file where title like ? order by subjectID");
pstmt.setString(1,"%"+title+"%");
//System.out.println("executing...");
ResultSet rs=pstmt.executeQuery();
//System.out.println("finish data scaning...");
/*
*插入每一个找到的主键到一个向量中
*/
while(rs.next()){
int fileID=rs.getInt("fileID");
v.addElement(new FilesPK(fileID));
}
/*
* 返回一个找到的主键的列举
*/
return v.elements();
}
catch(Exception e){
throw new FinderException(e.toString());
}
finally{
/*
* 为其他Bean释放数据库连接
*/
try{
if(pstmt!=null) pstmt.close();
}
catch(Exception e){}
try{
if(conn!=null) conn.close();
}
catch(Exception e){}
}
}
然后对表结构修改的时候就对这两个表同时修改,保证这两个表记录的是当前你的表结构。
从数据库读写的时候,首先从这两个表来确定读什么字段,怎么读。
* 利用 ResultSetMetaData 获得结果集中的字段数目 k=ResultSetMetaData.getColumnCount();<br>
* 定义vector[k]; 将每个字段的结果集放入 vector[k]中。<br>
* 定义二维数组String[k][v[0].size]; 将vector[k] copyinto 到相应的二维数组中<br>
* 返回的二维数组为:String【i】【j】 i=字段数目; j=单个字段的记录条数<br>
* @param rs ResultSet 作为参数<br>
* @return<br>
*/
public String[][] resultTwoArray(ResultSet rs)
{
try
{
ResultSetMetaData rsmd=rs.getMetaData();
int k=rsmd.getColumnCount(); // 取出字段数目
System.out.println("字段返回类型=|"+rsmd.getColumnTypeName(1)+"|");
System.out.println("########查询字段数目k="+k);
if(k<1)
{ String[][] nul=null;
return nul;
}
Vector v[]=new Vector[k];
for(int i=0;i<k;i++)
v[i]=new Vector(); while(rs.next())
{ for(int i=0;i<k;i++)
v[i].addElement(rs.getObject(i+1));
} if(v[0]==null||v[0].size()==0)
{ String[][] nul2=null;
return nul2;
}
String[][] arr=new String[k][v[0].size()];
for(int p=0;p<k;p++)
v[p].copyInto(arr[p]); // 将 Vector[p] copy 到数组 arr[p]中 //******************** null will be replaced with "" *************//
for(int i=0;i<arr.length;i++)
{ for(int j=0;j<arr[0].length;j++)
if(arr[i][j]==null)
arr[i][j]="";
}
return arr;
}
catch(Exception e)
{ e.printStackTrace();
}
return null;
}
我想用arraylist + string[col] 都不如你这个返回数组的好吧
对不起,java语法不太熟,那个vector 用不用在程序里释放掉?
在java中,资源是会自动回收的,这些是不用客户担心的。IF YOU HAVE ANY QUESTION ,YOU CAN CONTACT ME WITH E-MAIL:[email protected]