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
            }
        }
        
    }
  • 相关阅读:
    Github的基本使用
    Java Web工程搭建方法
    程序中使用百度地图
    Flutter 依赖的那些事儿
    Flutter Widgets (Container/Row/Column/Image)
    Flutter 安装
    UWP FillRowViewPanel
    UWP Composition API
    UWP Composition API
    UWP Jenkins + NuGet + MSBuild 手把手教你做自动UWP Build 和 App store包
  • 原文地址:https://www.cnblogs.com/xmb7/p/3680505.html
Copyright © 2011-2022 走看看