package ng.db; import java.sql.*; import java.util.*; import org.json.*; import org.json.JSONArray; public class DBSession implements AutoCloseable { private Connection con; //������ private boolean auto; //�Ƿ�Ϊ�Զ��ύ private Statement sql; //���һ��ʹ�õ�Statement public Connection getConnection(){ return con; } //���� private void reset(){ if(sql!=null){ try{ sql.close(); } catch(Exception e){ } sql=null; } } //��ѯ����� public class SelectResult{ private Statement state; private boolean closed; public Object getFirst() throws SQLException{ ResultSet r= state.getResultSet(); if(r.next()) return r.getObject(1); else return null; } //��ȡ��ǰ�Ľ�� public ResultSet getCurrentResult() throws SQLException { return state.getResultSet(); } public JSONObject allResultToJson() throws SQLException, JSONException{ JSONObject obj=new JSONObject(); int idx=1; do{ org.json.JSONArray a=this.resultToJson(false); obj.put("Table"+idx,a); idx++; }while(this.nextResult()); return obj; } //�ƶ�����һ�����,������ŷ���true public boolean nextResult() throws SQLException { return state.getMoreResults(); } public void close(){ if(closed==false){ closed=true; try{ state.close(); } catch(Exception e){ } } } //���캯�� public SelectResult(Statement state){ this.state=state; ResultSet set=null; } //����ǰ�Ľ����ֱ������JSON���� public org.json.JSONArray resultToJson(boolean RowAsObject) throws SQLException, JSONException{ return DBHelper.resultToJson(this.getCurrentResult(), RowAsObject); } public org.json.JSONArray resultToJson(String[] fields, boolean RowAsObject) throws SQLException, JSONException{ return DBHelper.resultToJson(fields,this.getCurrentResult(), RowAsObject); } public List allToJson(boolean RowAsObject) throws SQLException, JSONException{ List arrs=new ArrayList(); do{ arrs.add(DBHelper.resultToJson(this.getCurrentResult(), RowAsObject)); }while(this.nextResult()); return arrs; } } //��׼�洢���̵��ã���׼�洢���̶���,���ĵ� public class StdCallResult extends SelectResult{ public int ResultCode; public String ResultMessage; public StdCallResult(CallableStatement state,int ResultCode,String ResultMessage){ super(state); this.ResultCode=ResultCode; this.ResultMessage=ResultMessage; } } //����һ��SQL���� public void createSql(String sql) throws SQLException{ reset(); PreparedStatement result= con.prepareStatement(sql); this.sql=result; } public StdCallResult CallProc(String proc,Object... params) throws SQLException{ createStdCall(proc,params.length); return this.stdCall(params); } public void createQueryCall(String procName,int InputParamCount) throws SQLException { reset(); StringBuilder sp=new StringBuilder(); sp.append("{call "); sp.append(procName); sp.append("("); for(int i=0;i