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);
    		}
    	}
    }
    

      

  • 相关阅读:
    页面制作 Chapter 3--HTML
    页面制作 Chapter 2--开发、调试工具
    页面制作 Chapter 1--PhotoShop切图笔记
    打开AzureRay园子的大门,欢迎大家串门哟~
    自定义Console.log
    简单事件模型,JS防止单个函数异步重复调用
    C# 调用Restful 请求
    各类开源License说明
    面试JS
    gitignore
  • 原文地址:https://www.cnblogs.com/xiaziteng/p/4852591.html
Copyright © 2011-2022 走看看