zoukankan      html  css  js  c++  java
  • JDBC/连接池连接数据库

    import java.io.FileInputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.util.Properties;
    
    import org.apache.commons.dbcp.BasicDataSource;
    
    /**
     * 该类负责管理数据库的连接
     * 当业务逻辑需要使用数据库连接时,只需要通过当前类
     * 的静态方法获取连接即可。
     * 这样的好处在于,连接数据的工作由当前类维护,那么
     * 将来数据库连接发生改变时,只有这一个类改变就可以
     * 了。
     * @author Administrator
     *
     */
    public class DBUtil {
    	//DBCP 连接池
    	private static BasicDataSource ds;
    		
    	static{
    		try {
    			//读取配置文件
    			//java.util.Properties用来读取配置文件
    			Properties prop = new Properties();
    			//通过文件流读取并解析配置文件内容
    			prop.load(
    				new FileInputStream("config.properties")
    			);
    			/*
    			 * String getProperty(String key)
    			 * 根据配置文件中每一项的key("="左边的内容)
    			 * 获取对应的值("="右边的内容)
    			 */
    			String driverName = prop.getProperty("driverName");
    			String url = prop.getProperty("url");
    			String username = prop.getProperty("username");
    			String password = prop.getProperty("password");
    			//最大连接数
    			int maxActive = Integer.parseInt(
    					prop.getProperty("maxActive")
    			);
    			//最大等待时间
    			int maxWait = Integer.parseInt(
    					prop.getProperty("maxWait")
    			);
    			
    			ds = new BasicDataSource();
    			ds.setDriverClassName(driverName);
    			ds.setUrl(url);
    			ds.setUsername(username);
    			ds.setPassword(password);
    			//连接池中的最大连接数
    			ds.setMaxActive(maxActive);
    			//最大等待时间 getConnection时有效
    			ds.setMaxWait(maxWait);
    			
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		
    	}
    	
    	/**
    	 * 获取一个数据库连接
    	 * @return
    	 * @throws Exception
    	 */
    	public static Connection getConnection()
    														throws Exception{
    		try {
    			/*
    			 * 连接池提供了一个方法:
    			 * Connection getConnection()
    			 * 该方法会将当前连接池中的一个空闲连接返回
    			 * 由于连接池在创建时可以设置超时时间,该时间
    			 * 的作用就在这里体现,当连接池中没有空闲连接
    			 * 时,该方法会进入阻塞状态,等待控线连接,若
    			 * 设置的超时时间经过后,连接池依然没有空闲连接
    			 * 可用时,该方法会抛出超时异常。
    			 */
    			return ds.getConnection();
    		}catch(Exception e){
    			System.out.println("连接数据库异常");
    			throw e;
    		}	
    	}
    	
    	/**
    	 * DBUtil类中添加关闭数据库连接的代码
    	 * 
    	 */
    	public static void close(Connection conn){
    		if(conn!=null){
    			try {
    				conn.close();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	/**
    	 * 回滚数据库事务
    	 */
    	public static void rollback(Connection conn){
    		if(conn != null){
    			try {
    				conn.rollback();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	 
    	
    }
    

      

             

    连接池的需要导入jar包

    config.properties文本:

    #
    driverName=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/mytest1
    username=root
    password=123456
    #
    maxActive=5
    maxWait=5000

     向数据库中插入数据:

    import java.sql.Connection;
    import java.sql.Statement;
    
    public class mysqlInsert {
    
    	public static void main(String[] args) {
    		
    		Connection conn = null;
    		try{
    			conn = DBUtil.getConnection();
    			Statement st = conn.createStatement();
    			
    			String sql= "insert into yasuo "+
    			     "(name,salary,age) "+
    				"values "	+
    			     "('mhz',10000,21) ";
    			
    			System.out.println(sql);
    			st.executeUpdate(sql);
    			
    		}catch(Exception e){
    			e.printStackTrace();
    		}finally{
    			DBUtil.close(conn);
    		}
    	}
    }
    

      

  • 相关阅读:
    ajax专题
    luogu P1346 电车 最短路
    luogu P1462 通往奥格瑞玛的道路 最短路
    luogu P1328 生活大爆炸版石头剪刀布
    luogu P1315 联合权值 枚举
    luogu P1156 垃圾陷阱 背包问题
    luogu P1217 回文质数 枚举
    luogu P3650 滑雪课程设计 枚举
    luogu1209 修理牛棚 贪心
    luogu P1223 排队接水 贪心
  • 原文地址:https://www.cnblogs.com/xiaziteng/p/4852591.html
Copyright © 2011-2022 走看看