j2ee09:jdbc03:jdbc的最后封装(用到了反射)

1.实体类部分 package com.entity; import java.util.Date; public class News {  private int nid;
 private String ntitle;
 private String ncontent;
 private Date date;
 private String author;
 
 public int getNid() {
  return nid;
 }
 public void setNid(int nid) {
  this.nid = nid;
 }
 public String getNtitle() {
  return ntitle;
 }
 public void setNtitle(String ntitle) {
  this.ntitle = ntitle;
 }
 public String getNcontent() {
  return ncontent;
 }
 public void setNcontent(String ncontent) {
  this.ncontent = ncontent;
 }
 public Date getDate() {
  return date;
 }
 public void setDate(Date date) {
  this.date = date;
 }
 public String getAuthor() {
  return author;
 }
 public void setAuthor(String author) {
  this.author = author;
 }
 public News(int nid, String ntitle, String ncontent, Date date,
   String author) {
  super();
  this.nid = nid;
  this.ntitle = ntitle;
  this.ncontent = ncontent;
  this.date = date;
  this.author = author;
 }
 public News(int nid, String ntitle, String ncontent,
   String author) {
  super();
  this.nid = nid;
  this.ntitle = ntitle;
  this.ncontent = ncontent;
  this.author = author;
 }
 
 public News() {
  
 }
 @Override
 public String toString() {
  return "News [author=" + author + ", date=" + date + ", ncontent="
    + ncontent + ", nid=" + nid + ", ntitle=" + ntitle + "]";
 }
}   2.公共方法部分 package com.comm; import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class BaseDao {  
 private final String DB_DRIVER="oracle.jdbc.driver.OracleDriver";
 private final String DB_URL="jdbc:oracle:thin:@localhost:1521:XE";
 private final String DB_USER="xq";
 private final String DB_PWD="xq";
 
 
 Connection conn=null;
 PreparedStatement pstat=null;
 ResultSet res=null;
 
 
 /**
  * 获得数据库链接对象
  * **/
 public Connection getConn()
 {
  
  try
  {
   Class.forName(DB_DRIVER);
   conn=DriverManager.getConnection(DB_URL, DB_USER, DB_PWD);
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }
  return conn;
 }
 /**
  * 执行insert,update,delete语句
  * sql:执行的sql语句
  * param:给sql语句中?赋的值
  * int:受影响行数
  * **/
 public int executeUpdate(String sql,Object[]param)
 {
  int row=0;
  try
  {
   this.getConn();
   
   pstat=conn.prepareStatement(sql);
   
   if(param!=null&&param.length>0)
   {
    for(int x=0;x<param.length;x++)
    {
     pstat.setObject((x+1),param[x]);
    }
    
   }
   row = pstat.executeUpdate();
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }
  finally
  {
   this.closeAll(conn, pstat, res);
  }
  return row;
 }
 
 
 public List executeQuery(String sql,Object[]param,Class cls)
 {
  List list = new ArrayList();
  try
  {
   this.getConn();
   
   pstat=conn.prepareStatement(sql);
   
   if(param!=null&&param.length>0)
   {
    for(int x=0;x<param.length;x++)
    {
     pstat.setObject((x+1),param[x]);
    }
    
   }
   Field[]fs=cls.getDeclaredFields();
   res = pstat.executeQuery();
   while(res.next())
   {
    //1 创建对象
    Object obj=cls.newInstance();
    //2 给对象属性赋值
    
    for(int x=0;x<fs.length;x++)
    {
     Field f=fs[x];
     f.setAccessible(true);
     String type=f.getType().getName();
     if(type.equals("int"))
     {
      f.set(obj, res.getInt(x+1));
     }
     if(type.equals("java.lang.String"))
     {
      f.set(obj, res.getString(x+1));
     }
     if(type.equals("java.util.Date"))
     {
      f.set(obj, res.getDate(x+1));
     }
    }
    //3 将对象添加到集合
    list.add(obj);
    
   }
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }
  finally
  {
   this.closeAll(conn, pstat, res);
  }
  return list;
 }
 /**
  * 释放数据库访问资源
  * */
 public void closeAll(Connection conn,Statement stat,ResultSet res)
 {
  try
  {
   if(res!=null)
   {
    res.close();
   }
   if(stat!=null)
   {
    stat.close();
   }
   if(conn!=null)
   {
    conn.close();
   }
   
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }
 }
}   3.根据公共方法完成增删改查 package com.dao.impl; import java.util.List; import com.comm.BaseDao;
import com.dao.NewsDao;
import com.entity.News; public class NewsDaoImpl extends BaseDao implements NewsDao {  @Override
 public int saveNews(News news) {
  // TODO Auto-generated method stub
  String sql="insert into news values(?,?,?,sysdate,?)";
  Object []param={news.getNid(),news.getNtitle(),news.getNcontent(),news.getAuthor()};
  return super.executeUpdate(sql, param);
 
 }
 @Override
 public List<News> searchAllNews() {
  // TODO Auto-generated method stub
  String sql="select * from news";
  return super.executeQuery(sql, null, News.class);
  
 }
 
 @Override
 public int batDeleteNews(Integer[] nid) {
  String sql="delete from news where nid in(";
  for(int x=0;x<nid.length;x++)
  {
   if(x==nid.length-1)
   {
    sql=sql+"?)";
   }
   else
   {
    sql=sql+"?,";
   }
  }
  return super.executeUpdate(sql,nid);
 }
 
 
 public static void main(String[] args) {
  NewsDao dao = new NewsDaoImpl();
  /*News news = new News();
  news.setNid(10);
  news.setNtitle("新闻标题");
  news.setNcontent("新闻内容");
  news.setAuthor("作者");
  int row = dao.saveNews(news);
  if(row>0)
  {
   System.out.println("----成功");
  }
  else
  {    System.out.println("----失败");
  }*/
  
 /* List<News> list=dao.searchAllNews();
  for(News n:list)
  {
   System.out.println(n);
  }*/
  
  
  int row = dao.batDeleteNews(new Integer[]{1,2});
  if(row>0)
  {
   System.out.println("----成功");
  }
  else
  {    System.out.println("----失败");
  }
 } }      

猜你喜欢

转载自1601844782.iteye.com/blog/2271792