数据库连接bean

package com;

import java.io.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
import java.lang.*;
import java.net.*;
public class DBConn implements Serializable {

  Connection con;
  PreparedStatement pstmt;
  Statement stmt;
  ResultSet rst;

  String url="jdbc:oracle:thin:@192.168.0.130:1521:oa";
  String user="zzoa";
  String password="zzoa";


  public DBConn() {
  }

  //--------------------------------------------------------------------------
  public Connection getConnection(){
    if(openConnection()){
      return this.con;
    }
  return con;
  }
  //-----------------------------------------------------------------------------
public int getItemID(){
    int par=-1;
    CallableStatement proc = null;
    if(openConnection()){
      try{

        proc = con.prepareCall("{call getdizeng(?)}");
        proc.registerOutParameter(1, Types.BIGINT);
        proc.execute();
        par = proc.getInt(1);
      }catch(Exception e){e.printStackTrace();}
      finally{
        try{
          if (proc != null)
            proc.close();
        }catch(Exception e){}
      }

    }
    return par;
  }
  //--------------------------------------------------------------------------
  public boolean openConnection() //建立数据库的连接
   {
     boolean flag=true;
     try
     {
       if((con!=null)&&!con.isClosed()) return true;
         Class.forName("oracle.jdbc.OracleDriver").newInstance();
         con = DriverManager.getConnection(url, user, password);
         stmt=con.createStatement();

     }
     catch(Exception e)
     {
       e.printStackTrace();
       flag=false;
     }
     return flag;
   }

  public void close()
  {
    try
    {
      if(rst!=null)   rst.close();
      if(stmt!=null)  stmt.close();
      if(con!=null&&!con.isClosed())
        con.close();
    }
    catch(Exception ee)
    {
     ee.printStackTrace();
    }
  }

/
  public void executeUpdate(String sql) //按条件删除指定表的行
{
 if(this.openConnection())
 {
   try
   {
     stmt=con.createStatement();
     stmt.executeUpdate(sql);
      this.close();
   }
   catch(Exception e5)
   {
    System.out.println("Exception:"+e5.getMessage().toString());
   }
 }
 else
 {
   System.out.println("连接异常!");
 }

}



  public ResultSet executeQuery(String sql) //按条件查询指定表中的所有数据
  {
    if(this.openConnection())
    {
      try
      {
        rst=stmt.executeQuery(sql);

      }
      catch(Exception e8)
      {
        e8.printStackTrace();
      }
    }
    else
    {
      System.out.println("连接异常!");
      rst=null;
    }
    return rst;
  }

  public boolean next() throws SQLException
  {
    return rst.next();
  }
  public void executeInsertsql(String sql) //向表中插入数据
{

  if(this.openConnection())
  {
    try
    {

      stmt.executeUpdate(sql);

    }
    catch(Exception e6)
    {
      e6.printStackTrace();
    }
  }
  else
  {
    System.out.println("连接异常!");
  }
}

 

 

  public String getData(String colName) throws SQLException
  {
    return  rst.getString(colName).trim();
  }
  public String getData(int intName) throws SQLException
  {
    return  rst.getString(intName).trim();
  }

 


  public static void main(String[] args)throws Exception{

    System.out.println("par:"+0x10000);


  }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值