Prodigy/kf智
          我的博客
          posts - 20,comments - 7,trackbacks - 0

          package DBConnection_Sql;

          import java.sql.PreparedStatement;
          import java.sql.ResultSet;
          import java.sql.ResultSetMetaData;
          import java.sql.SQLException;
          import java.util.HashMap;

          import dao_sql.WebBean;

          /**
           * @author Administrator
           *
           * TODO 要更改此生成的類型注釋的模板,請轉至
           * 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
           */
          public class DBConnection extends WebBean 
          {
           public String []FieldName;//存放數據庫中的字段名字
           public int CountField=0;//表示有多少個字段
           public DBConnection()
           {
           }
           public String gb2iso(String qs)
           {
            try
            {
             if (qs == null)
              return "NULL";
             else
             {
              String newStr= new String(qs.getBytes("gb2312"),"iso8859-1");
              return newStr;
             }
            }
            catch(Exception e)
            {
              System.err.println("gb2iso error:"+e.getMessage());
            }
            return "NULL";
           }
           public String iso2gb(String qs)
           {
            try
            {
             if (qs == null)
              return "NULL";
             else
             {
              String newStr= new String(qs.getBytes("iso8859-1"),"gb2312");
              return newStr;
             }
            }
            catch(Exception e)
            {
              System.err.println("iso2gb error:"+e.getMessage());
            }
            return "NULL";
           }
           public boolean ModiData(String Sql)
           {
            PreparedStatement pstmt = null;
            HashMap hm=new HashMap();
            try
            {
             if (!getConnection())
             {
              return false;
             }
             pstmt = conn.prepareStatement(Sql);
             int i = pstmt.executeUpdate();
             pstmt.close();
            }
            catch (Exception e)
            {
             println(e);
             return false;
            }
            finally
            {
             free(pstmt);
            }
            return true;
           }
           //根據SQL語句來把查詢的結果放在HASHMAP里
           public HashMap GetData(String Sql)
           {
            PreparedStatement pstmt = null;
            HashMap hm=new HashMap();
            try
            {
             if (!getConnection())
             {
              return null;
             }
             pstmt = conn.prepareStatement(Sql);
             hm= getRS(pstmt.executeQuery());
            }
            catch (Exception e) {
             println(e);
            } finally {
             free(pstmt);
            }
            return hm;
           }
           
           //得到總的查詢記錄
           public int getCount(String sql)
           {
            PreparedStatement pstmt = null ;
            int count = 0 ;
            try
            {
             if(!getConnection())
             {
              return 0 ;
             }
             pstmt = conn.prepareStatement(sql);
             count = getCount(pstmt.executeQuery()) ;
            } catch (Exception e)
            {
             println(e);
            }
             finally
             {
             free(pstmt);
            }
            return count ;
           }

           //根據表名返回字段名
           private void GetFiels(String TableName)
           {
            PreparedStatement pstmt = null;
            HashMap hm=new HashMap();
            String SqlStr="select * from "+TableName;
            try
            {
             pstmt = conn.prepareStatement(SqlStr);
             hm= getRS(pstmt.executeQuery());
             ResultSet rs = pstmt.executeQuery();
             ResultSetMetaData Temp=rs.getMetaData();
             CountField=Temp.getColumnCount();
             FieldName=new String[CountField];
             for(int j=0;j<CountField;j++)
             {
              FieldName[j]=Temp.getColumnName(j+1);
             }
             rs.close();
             pstmt.close();
             return;
            }
            catch (Exception e)
            {
             println(e);
            }
           }

           //根據表名字把字段和字段的數目放到FieldName 和 CountField
           public void GetFiels2(String TableName)
           {
            PreparedStatement pstmt = null;
            HashMap hm=new HashMap();
            String SqlStr="select * from "+TableName;
            try
            {
             if (!getConnection())
             {   
              return;
             }
             pstmt = conn.prepareStatement(SqlStr);
             hm= getRS(pstmt.executeQuery());
             ResultSet rs = pstmt.executeQuery();

             ResultSetMetaData Temp=rs.getMetaData();//獲得RESULTSET中的表格數據類型
             
             CountField=Temp.getColumnCount();       //獲得 ResultSet 中的列數。
             FieldName=new String[CountField];
             for(int j=0;j<CountField;j++)
             {
              FieldName[j]=Temp.getColumnName(j+1);//獲得列名。
              System.out.print(FieldName[j]);
             }
             rs.close();
             pstmt.close();
             return;
            }
            catch (Exception e)
            {
             println(e);
            }
           
            return;
           }
          //用來執行inserto update delete
           public void GetFieldBySql(String SqlStr)
           {
            PreparedStatement pstmt = null;
            HashMap hm=new HashMap();
            try
            {
             if (!getConnection())
             {   
              return;
             }
             pstmt = conn.prepareStatement(SqlStr);
             hm= getRS(pstmt.executeQuery());
             ResultSet rs = pstmt.executeQuery();
             ResultSetMetaData Temp=rs.getMetaData();
             CountField=Temp.getColumnCount();
             FieldName=new String[CountField];
             for(int j=0;j<CountField;j++)
             {
              FieldName[j]=Temp.getColumnName(j+1);
             }
             rs.close();
             pstmt.close();
             return;
            }
            catch (Exception e)
            {
             println(e);
            }
            finally
            {
             free(pstmt);
            }
            return;
           }
          //根據表名 你要添加的數據 對數據庫進行添加功能
           public boolean AddData(String Table,Object[] Data)
           {
            String sqlstr1 = "select * from "+Table ;
            
            int sss = 0;
            PreparedStatement pstmt = null;
            try {
             try {
              if(!getConnection())
              {
               return false;
              }
             } catch (Exception e2) {
             
              e2.printStackTrace();
             }
             pstmt = conn.prepareStatement(sqlstr1) ;
            } catch (SQLException e1) {
            
             e1.printStackTrace();
            }
           
            ResultSet rs = null;
            try {
             rs = pstmt.executeQuery();
            } catch (SQLException e2) {
            
             e2.printStackTrace();
            }
            ResultSetMetaData Temp = null;
            try {
             Temp = rs.getMetaData();
            } catch (SQLException e3) {
             
             e3.printStackTrace();
            }
            try {
             if(Temp.isAutoIncrement(1))
             {
              sss = 1 ;
             }
             else
             {
              sss = 0 ;
             }
            } catch (SQLException e4) {
            
             e4.printStackTrace();
            }
            
            HashMap hm=new HashMap();
            try
            {
             
             GetFiels(Table); 
             String SqlStr = "insert "+Table+"(";
             
             for(int i=sss;i<CountField;i++)
             {
             
              SqlStr+=FieldName[i];
              if(i<CountField-1)
              {
               SqlStr+=",";
              }
              else
              {
               SqlStr+=")values(";
              }
             }
             for(int i=sss;i<CountField;i++)
             {
              SqlStr+="?";
              if(i<CountField-1)
              {
               SqlStr+=",";
              }
              else
              {
               SqlStr+=")";
              }
             }
             pstmt = conn.prepareStatement(SqlStr);
             for(int i=0;i<CountField-sss;i++)
             {
              if(Data[i].getClass().getName().equals("java.lang.String"))
              {
               String sTemp=gb2iso(Data[i].toString());
               pstmt.setString(i+1,sTemp);
              }
              if(Data[i].getClass().getName().equals("java.lang.Integer"))
              {
               pstmt.setObject(i+1,Data[i]);
              }
             }
             pstmt.executeUpdate();
             pstmt.close();
             conn.close();
            }
            catch(Exception e)
            {
             println(e);
             return false;   
            }
           
            return true;
           }
           //數據庫的表名 字段名  和你要修改的數據 條件 來進行對數據庫的 修改
           public boolean ModiData(String Table, String[] Fields,Object[] Data,String Limit)
           {
            PreparedStatement pstmt = null;
            HashMap hm=new HashMap();
            try
            {
             if(!getConnection())
             {
              return false;
             }
             int m=Fields.length;
             
             String SqlStr="update "+Table+" set ";
             for(int i = 0;i < m;i++)
             {
              SqlStr+= Fields[i]+"=?";
              if(i<m-1)
              {
               SqlStr+=",";
              }
              else
              {
               SqlStr+=" ";
              }
             }
             SqlStr+=Limit;
            
             pstmt = conn.prepareStatement(SqlStr);
             for(int i=0;i<m;i++)
             {
              if(Data[i].getClass().getName().equals("java.lang.String"))
              {
               String sTemp=Data[i].toString();
               
               pstmt.setString(i+1,sTemp);
              }
              if(Data[i].getClass().getName().equals("java.lang.Integer"))
              {
               pstmt.setObject(i+1,Data[i]);
              }
             }
             pstmt.executeUpdate();
             pstmt.close();
             conn.close();
            }
            catch (Exception e)
            {
             println(e);
            }
            return true;
           }
           //進行修改Table表名,Fields[]字段名 Data[]字段數據,Limit修改的條件
           public boolean ModiData(String Table,Object[] Data,String Limit)
           {
            PreparedStatement pstmt = null;
            HashMap hm=new HashMap();
            try
            {
             if (!getConnection())
             {
              return false;
             }
             GetFiels(Table);
             if(CountField!=Data.length)
             {
              return false;
             }
             String SqlStr = "update "+Table+" set ";
             
             for(int i = 0;i < CountField;i++)
             {
              SqlStr+= FieldName[i]+"=?";
              if(i<CountField-1)
              {
               SqlStr+=",";
              }
              else
              {
               SqlStr+=" ";
              }
             }
             SqlStr+=Limit;
             System.out.println(SqlStr);
             pstmt = conn.prepareStatement(SqlStr);
             
             for(int i=0;i<CountField;i++)
             {
              if(Data[i].getClass().getName().equals("java.lang.String"))
              {
               String sTemp=Data[i].toString();
               pstmt.setString(i+1,sTemp);
              }
              if(Data[i].getClass().getName().equals("java.lang.Integer"))
              {
               pstmt.setObject(i+1,Data[i]);
              }
             }
             System.out.println(SqlStr);
             pstmt.executeUpdate();
             pstmt.close();
             conn.close();
            

            } catch (Exception e)
            {
             println(e);
            }
            finally
            {
             free(pstmt);
            }
            return true;
           }
          }

          posted on 2006-03-29 15:50 xiaozhi 閱讀(284) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 博白县| 福鼎市| 乌兰察布市| 娱乐| 玉树县| 海安县| 玉溪市| 于田县| 普陀区| 梅州市| 类乌齐县| 尼木县| 木里| 新宾| 海城市| 平潭县| 敖汉旗| 凤山市| 江都市| 淮北市| 铁力市| 洛浦县| 明光市| 塔城市| 乃东县| 福清市| 扎囊县| 偃师市| 和顺县| 剑川县| 闵行区| 遂溪县| 西和县| 乌拉特后旗| 卓资县| 苍山县| 九龙县| 施秉县| 澄城县| 兴海县| 南郑县|