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 阅读(270) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: