zoukankan      html  css  js  c++  java
  • sql server操作类(本人自己写的)

    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
            }
        }
        
    }
  • 相关阅读:
    内存对齐
    C++中构造函数
    计算机视觉领域的大牛主页
    各种银行卡的收费情况
    常识
    毕业生必须知道
    计算机视觉领域资料
    人际关系
    生活常识
    可使用在项目的web gantt甘特图有哪些?
  • 原文地址:https://www.cnblogs.com/xmb7/p/3680505.html
Copyright © 2011-2022 走看看