无信号  
package com.mytest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
public class DBUtil {
    
    //定义连接数据库需要的
    Connection ct=null;
    PreparedStatement pS=null;
    ResultSet rS=null;
    
    public ResultSet getSlect(String sql,Object ...params){
        Vector rowDate=new Vector();
        Vector columnDate =new Vector();
        try {
            ct = connectWithDB();
            pS=ct.prepareStatement(sql);
            for(int i = 0;i < params.length;i++){
                pS.setObject(i+1, params[i]);
            }
            rS=pS.executeQuery();
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            return rS;
        }
    }
    
    /************修改数据库操作*********************/
    public int update(String sql,Object ...params){
        int executeUpdate_int = 0;
        try {
            ct = connectWithDB();
            pS=ct.prepareStatement(sql);
            for(int i = 0;i < params.length;i++){
                pS.setObject(i+1, params[i]);
            }
            //执行操作
            executeUpdate_int = pS.executeUpdate();
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            shutDownDB();
            return executeUpdate_int;
        }
    }
    
    /************连接数据库*********************/
    private Connection connectWithDB(){
        Connection connection = null;
        try {
            //加载驱动
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            connection =  DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=spdb","sa","123456");
        } catch (Exception e) {
            // TODO: handle exception
        }
        return connection;
    }
    
    /************关闭数据库的相关连接*********************/
    public void shutDownDB(){
        try
        {
            if(rS!=null) rS.close();
            if(pS!=null) pS.close();
            if(ct!=null) ct.close();
        } catch (Exception e2)
        {
            e2.printStackTrace();
            // TODO: handle exception
        }
    }
    
}

 

posted on 2014-04-22 14:40  BenXian  阅读(1733)  评论(0编辑  收藏  举报