利用javabean轻松实现对数据库的连接、查询以及增删改

在做jsp开发的时候,经常需要对数据库进行查询及增删改,而这些操作使用的又是非常频繁
写了一个javabean,来实现与数据库的连接以及对数据库的增删改

QueryBean.java:

pacakage operate_db;
/**
* Title: select insert update and delete
* Description: select insert update and delete
* Copyright: zhizhilizi
* @authorzhizhilizi
* @version 1.0
*/

import java.sql.*;
public class QueryBean
{
public String query_statement; /*定义sql语句*/
public String param[]; /*查询条件,或者是新的记录*/
public ResultSet result=null;
public Connection conn;

public void setParam(String[] param)
{
      this.param=param;
}
public void setQuerystatement(String query_statement)
{
       this.query_statement=query_statement;
}
public void setConnection(String driverName,String jdbcURL,String username,String passwd) throws Exception
{
      Connection conn1;
      Class.forName(driverName);
      conn1=DriverManager.getConnection(jdbcURL,username,passwd);
      conn1.setAutoCommit(false);
      this.conn = conn1;
}
/*获取查询结果*/
public ResultSet getResult()
{
       try
     {
        PreparedStatement select_stm=conn.prepareStatement  (query_statement,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
        if (param!=null)
          for(int i=0;i<param.length;i++)
         select_stm.setString(i+1,param);
          result=select_stm.executeQuery();
     }catch(Exception e){System.out.println(e);}
  
     return result;
}

/*对数据库进行增加记录操作*/
public void insertRecord() throws SQLException,java.io.UnsupportedEncodingException
{
       try
          {
             PreparedStatement insert_stm=conn.prepareStatement(query_statement);
             if (param!=null)
             for(int i=0;i<param.length;i++)
             insert_stm.setString(i+1,param);
             insert_stm.executeUpdate();
             insert_stm.close();
             conn.commit();
           }
       catch(Exception e)
          {
             System.out.println(e);
             conn.rollback();
          }
}

/*对数据记录进行更改操作*/
public void updateRecord() throws SQLException,java.io.UnsupportedEncodingException
{
           try
              {
               PreparedStatement update_stm=conn.prepareStatement(query_statement);
              if (param!=null)
              for (int i=0;i<param.length;i++)
              update_stm.setString(i+1,param);
              update_stm.executeUpdate();
              update_stm.close();
              conn.commit();
              }
             catch(Exception e)
             {
              System.out.println(e);
               conn.rollback();
             }
}
/*删除数据记录*/
public void deleteRecord() throws SQLException,java.io.UnsupportedEncodingException
{
     try
       {
          PreparedStatement delete_stm=conn.prepareStatement(query_statement);
          if (param!=null)
           for (int i=0;i<param.length;i++)
           delete_stm.setString(i+1,param);
            delete_stm.executeUpdate();
            delete_stm.close();
            conn.commit();
       }
    catch(Exception e)
        {
          System.out.println(e);
            conn.rollback();
        }
}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值