spring配置数据源连接池(c3p0)

我是maven工程

spring配置数据源连接池(c3p0)_第1张图片


在数据库,先建一个表,初始化3条数据。

CREATE TABLE `t_user` (
  `_id` INT(11) NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(50) DEFAULT NULL,
  `userpwd` VARCHAR(32) DEFAULT NULL,
  PRIMARY KEY (`_id`)
) ENGINE=INNODB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;


SELECT * FROM t_user

INSERT INTO `t_user` VALUES ('1', 'jake', '123456');
INSERT INTO `t_user` VALUES ('2', 'rose', '123456789');
INSERT INTO `t_user` VALUES ('3', 'tom', '999');

application.xml

  
        
      
         
        
           
        
      
   
     
           
	          
	          
	          
	          
      
    
         
    


 


创建连接的类

package com.broadtech.bdvmonitor.utils;


import java.sql.Connection;
import java.sql.SQLException;


import javax.sql.DataSource;


import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;


import com.broadtech.bdvmonitor.main.Main;


public class SpringDataSource {


	private static Logger logger = Logger.getLogger(Main.class);
	 
//这里是spring ioc, 取出数据源,初始化
       static DataSource dataSource = (DataSource) Main.context.getBean("dataSource");
    
    
	public DataSource getDataSource() {
		return dataSource;
	}
 
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}


	public static  Connection getConnection() {
		
		Connection conn=null;
		try {
			conn= dataSource.getConnection();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block 
			logger.error("spring数据源连接有异常",e);
		}
		return conn ;
	}


	 
}

spring配置在main方法中,初始化

 

package com.broadtech.bdvmonitor.test;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


import javax.sql.DataSource;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;


public class TestSpringJdbc {
    
    @Test
    public void demo(){
        String xmlpath = "springconfig/application.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(xmlpath);
        JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbctemplate");
        jdbcTemplate.update("insert into t_user(username,userpwd) values(?,?)", "tom2","362427");
    }
    


    PreparedStatement st= null;
    Connection conn=null;
    
    @Test
    public void demo1(){
        String xmlpath = "springconfig/application.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(xmlpath);


        DataSource dataSource = (DataSource) context.getBean("dataSource");
        
      
	try {
		conn = dataSource.getConnection();
	    String sql="insert into t_user(username,userpwd) values(?,?)";
	      st=conn.prepareStatement(sql); 
	      st.setString(1, "孟梁");
	      st.setString(2, "spring测试成功2");
	      st.addBatch();
	      st.executeBatch();
		
		
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
        
    
       
        
    }
    
    


}


运行结果:

spring配置数据源连接池(c3p0)_第2张图片

你可能感兴趣的:(java)