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

    首先设置下配置文件:取名db.properties
    #mysql注释
    mysql_driver=com.mysql.jdbc.Driver
    mysql_url=jdbc:mysql://127.0.0.1:3306/test
    mysql_user=root
    mysql_pwd=123456
    
    #连接池相关参数
    #初始化连接数
    dataSource.initialSize=10
    #最大空闲数
    dataSource.maxIdle=5
    #最小空闲数
    dataSource.minIdle=2
    #最大连接数
    dataSource.maxActive=8
    #最小连接数
    dataSource.minActive=3
    #超时时间1分钟
    #单位是毫秒
    dataSource.maxWait=60000
    这是调用以上配置文件的连接数据库的代码:取名X_DBUtil
    package net.picp.mywebsite.util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class X_DBUtil {
    	private static String driver=null;
    	private static String url=null;
    	private static String user=null;
    	private static String password=null;
    	//读取文件的对象
    	private static Properties ps=new Properties();
    	//静态代码块,类加载的时候,只需要执行一次
    	static{
    		try {
    			ps.load(X_DBUtil.class.getClassLoader().getResourceAsStream("db.properties"));
    			driver=ps.getProperty("mysql_driver");
    			url=ps.getProperty("mysql_url");
    			user=ps.getProperty("mysql_user");
    			password=ps.getProperty("mysql_pwd");
    			//加载驱动
    			Class.forName(driver);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    //创建连接方法
    	public static Connection getConnection() throws SQLException{
    		return DriverManager.getConnection(url, user, password);
    	}
    //关闭连接方法
    	public static void closeConnection(Connection con,Statement st,ResultSet rs){
    		try {
    				if(rs!=null){
    				rs.close();
    				}
    				if(st!=null){
    					st.close();
    				}
    				if(con!=null){
    					con.close();
    				}
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		
    	}
    	
    
    }
    这是调用以上配置文件连接池的连接数据库的代码:取名X_DBCPUtil
    package net.picp.mywebsite.util;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    import org.apache.commons.dbcp.BasicDataSource;
    
    /**
     * 工具类:读取.properties文件数据,加载驱动创建连接,关闭
     * 连接池实现连接,关闭,提高性能
     * @author Administrator
     *
     */
    public class X_DBCPUtil {
    	private static String driver=null;
    	private static String url=null;
    	private static String user=null;
    	private static String password=null;
    	//读取文件的对象
    	private static Properties ps=new Properties();
    	private static BasicDataSource ds=null;
    	//静态代码块,类加载的时候,只需要执行一次
    	static{
    		try {
    			ps.load(X_DBCPUtil.class.getClassLoader().getResourceAsStream("db.properties"));
    			driver=ps.getProperty("mysql_driver");
    			url=ps.getProperty("mysql_url");
    			user=ps.getProperty("mysql_user");
    			password=ps.getProperty("mysql_pwd");
    			
    			//获取连接池相关参数
    			String initialSize=ps.getProperty("dataSource.initialSize");
    			String maxIdle=ps.getProperty("dataSource.maxIdle");
    			String minIdle=ps.getProperty("dataSource.minIdle");
    			String maxActive=ps.getProperty("dataSource.maxActive");
    			String minActive=ps.getProperty("dataSource.minActive");
    			String maxWait=ps.getProperty("dataSource.maxWait");
    			
    			ds=new BasicDataSource();
    			ds.setDriverClassName(driver);
    			ds.setUrl(url);
    			ds.setUsername(user);
    			ds.setPassword(password);
    			if(initialSize!=null){
    				ds.setInitialSize(Integer.parseInt(initialSize));			
    			}
    			if(maxIdle!=null){
    				ds.setMaxIdle(Integer.parseInt(maxIdle));
    			}
    			if(minIdle!=null){
    				ds.setMinIdle(Integer.parseInt(minIdle));
    			}
    			if(maxActive!=null){
    				ds.setMaxActive(Integer.parseInt(maxActive));
    			}
    			if(minActive!=null){
    				//DBCP没有提供最小连接数的方法
    			}
    			if(maxWait!=null){
    				ds.setMaxWait(Long.parseLong(maxWait));
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    //创建连接方法
    	public static Connection getConnection() throws SQLException{
    		Connection con=null;
    		if(ds!=null){
    			con=ds.getConnection();
    		}
    		return con;
    	}
    //归还连接方法
    	public static void releaseConnection(Connection con,Statement st,ResultSet rs){
    		try {
    				if(rs!=null){
    				rs.close();
    				}
    				if(st!=null){
    					st.close();
    				}
    				if(con!=null){
    					//归还
    					con.close();
    				}
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		
    	}
    	
    /*	public static void main(String[] args){
    		try {
    			System.out.println(getConnection());
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    	}*/
    	
    }
    

      注意:需要用到的jar包

    commons-dbcp.jar
    commons-pool.jar
    dom4j-1.6.1.jar
    mysql-connector-java-5.1.8-bin.jar
    ojdbc6.jar
    

      

      

  • 相关阅读:
    老爹回忆录
    【日常】你这一辈子,有没有被保安拦住过

    【论文阅读】Deep Adversarial Subspace Clustering
    【日常】爱情故事应该是这样的
    maven打包遇到错误,Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test
    Thymeleaf th:include、th:replace引用
    IDEA中log4j.properties配置文件详解
    错误Uncaught Error: Bootstrap's JavaScript requires jQuery at bootstrap.min.js:6 错误详解
    ybatis 逆向工程 自动生成的mapper文件没有 主键方法
  • 原文地址:https://www.cnblogs.com/yingyigongzi/p/9162882.html
Copyright © 2011-2022 走看看