javabean数据库操作类包

 
package  dbconn;

import  java.sql. * ;

public   class  DBResult
{
    
private Connection con;
    Statement stmt;
    ResultSet rs 
= null
    
public  DBResult(){
        
try {
            
this.con=getConnection();
        }
 catch (Exception e) {
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

    }

    
    
public static Connection getConnection() throws Exception{
        Connection con
=null;
        
try{
        
//    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        
//    con=DriverManager.getConnection("jdbc:odbc:mycms","sa","");
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            con
=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_shopping;User=sa;Password=");
        }
catch(ClassNotFoundException e){
            System.out.println(e.getMessage());   
        }
catch(SQLException e){
            System.out.println(e.getMessage());   
        }
 
        
if(con==null)
            System.out.println(
"error");   
        
return con;
    }

    
    
/**
     * 用于获得执行SQL语句的ResultSet对象
     
*/

    
public ResultSet getResult(String sql){
        
try{
            con
=getConnection();
            stmt
=con.createStatement();
            rs
=stmt.executeQuery(sql);
        }

        
catch(Exception e){}
        
return rs;
    }

    
    
/**
     * 用于执行SQL语句没有返回值
     
*/

    
public int executeUpdate(String sql) throws SQLException{
        
int result=0
        
try {
            con
=getConnection();
            stmt
=con.createStatement();    
            stmt.executeUpdate(sql);
        }
 catch (Exception e) {
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

        
return result;
    }

    
    
/**
     * 用于获得执行SQL语句的PreparedStatement(预处理)对象
     
*/

    
public PreparedStatement getPreparedStatement(String sql){
        
try{
            con
=getConnection();
            PreparedStatement pstmt
=con.prepareStatement(sql);
            
return pstmt;
        }

        
catch(Exception e){}
        
return null;
    }

    
    
/**
     * 关闭连接
     
*/

    
public void closeCon(){
        
try{
            
this.con.close();
        }
catch(Exception e){
            e.printStackTrace();
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值