• java操作ORACLE数据库工具类(JDBC)


    package com.util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    /**
     *	
     * 2015-3-5下午2:45:56
     *
     *MusicWeb.util.DBUtil
     *连接数据库 类
     */
    public class DBUtil {
    	
    
    		private static String driver = "oracle.jdbc.driver.OracleDriver";
    
    		private String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    
    		private String user = "XXX";//oracle数据库的用户名
    		private String pwd = "XXXXXX";//oracle数据库的用户密码
    		private PreparedStatement sta = null;
    		private ResultSet rs = null;
    		private Connection conn = null;
    
    		/**
    		 * 加载驱动程序
    		 */
    		static {
    			try {
    				Class.forName(driver);
    			} catch (ClassNotFoundException e) {
    				e.printStackTrace();
    			}
    		}
    
    		/**
    		 * @return 连接对象
    		 */
    		public Connection getConn() {
    			try {
    				conn = DriverManager.getConnection(url, user, pwd);
    			} catch (SQLException e) {
    
    				e.printStackTrace();
    			}
    			return conn;
    		}
    
    		/**
    		 * @param sql
    		 *            sql语句  增加,删除,修改
    		 * @param obj
    		 *            参数
    		 * @return
    		 */
    		public int update(String sql, Object... obj) {
    			int count = 0;
    			conn = getConn();
    			try {
    				sta = conn.prepareStatement(sql);
    				if (obj != null) {
    					for (int i = 0; i < obj.length; i++) {
    						sta.setObject(i + 1, obj[i]);
    					}
    				}
    				count = sta.executeUpdate();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			} finally{
    			
    				close();
    			}
    			return count;
    		}
    
    		/**
    		 * @param sql sql语句
    		 * @param obj 参数
    		 * @return 数据集合
    		 */
    		public ResultSet Query(String sql,Object...obj){
    			conn=getConn();
    			try {
    				sta=conn.prepareStatement(sql);
    				if(obj!=null){
    					for(int i=0;i<obj.length;i++){
    						sta.setObject(i+1, obj[i]);
    					}
    				}
    				rs=sta.executeQuery();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    			return rs;
    		}
    		
    		/**
    		 * 关闭资源
    		 */
    		public void close() {
    			try {
    				if (rs != null) {
    					rs.close();
    				}
    			} catch (SQLException e) {
    				e.printStackTrace();
    			} finally {
    				try {
    					if (sta != null) {
    						sta.close();
    					}
    				} catch (SQLException e2) {
    					e2.printStackTrace();
    				} finally {
    					if (conn != null) {
    						try {
    							conn.close();
    						} catch (SQLException e) {
    							e.printStackTrace();
    						}
    					}
    				}
    			}
    		}
    
    }
    
    补充:JDBC深度封装的工具类 http://blog.sina.com.cn/s/blog_7ffb8dd501013qd5.html

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    What Apache ZooKeeper is and when should it be used?
    基于Apache Curator框架的ZooKeeper使用详解
    Curator典型应用场景之Master选举
    Curator典型应用场景之事件监听
    Curator典型应用场景之分布式锁
    Zookeeper原生Java API、ZKClient和Apache Curator 区别对比
    IntelliJ IDEA oneline function formatting
    Converting a List to String in Java
    UNIAPP 微信小程序做H5 PDF预览
    Git 常用命令,持续更新
  • 原文地址:https://www.cnblogs.com/lovelyx/p/4867097.html
Copyright © 2020-2023  润新知