与Sql连接的javabean

package com.bwm.dbc;
import java.sql.*;
public class Conn{
private static Connection con;
private Statement stmt;
private ResultSet rs;
private static final string drivenname="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//数据库连接URL
private static final String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;Database
Name=pubs;user=sa;password=;"123";
/*获取数据库的连接*/
public static synchronized Connection getCOn()throws Exception{
  try{
//加载驱动程序
     Class.forName(drivename);
//获取数据库连接
     con=DriveManager.getConnection(url);
     return con;
    }catch(SQLException e){
         System.err.println(e.getMessage());
         throw e;
    }  
}
/*获取Statement只能用于查询的语句*/ 
public Statement getStmtread(){
         try{
                con=getCon();
               //创建Statement对象
           stmt=con.createStatement(resultSet.TYPE_SCROLL_INSENSITIVE,
                                    ResultSet.CONCUR_READ_ONLY);
            return null;
           }catch(Exception e){
                           System.err.println(e.getMessage());
                           e.printStackTrace();
                    }
             return null;
         }
/* 获取result*/
public ResultSet getRs(string sql) {
         try{
           stmt=getStmtread();
//创建ResultSet对象
              rs=stmt.excuteQuery(sql);
              return rs;
            }catch(Exception e){
                           System.err.println(e.getMessage());
                           e.printStackTrace();
                    }
             return null;
         }
/*获取Statement用于删除,更新,和添加的SQL语句*/
      
     public Statement getStmt(){
        try{
             con=getCon();
             stmt=con.createStatement();
             return  stmt;
                }catch(Exception e){
                           System.err.println(e.getMessage());
                           e.printStackTrace();
                    }
             return null;
         }  
/*关闭数据库的连接*/
       public synchronized void close(){
          try{
               if(rs!=null){
                    rs.close();
                    rs=null;
                   }    
                  }catch(Exception e){
                           System.err.println(e.getMessage());
                           e.printStackTrace();
                    }
          
             try{
                  if(stmt!=null){
                   stmt.close();
                   stmt=null;
                        }
                  }catch(Exception e){
                           System.err.println(e.getMessage());
                           e.printStackTrace();
                    }
          try{
               if(con!=null){
                    con.close();
                    con=null;
                    }
                  }catch(Exception e){
                           System.err.println(e.getMessage());
                           e.printStackTrace();
                    }
             }
         }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值