基于Maven的Spring 连接c3p0实例

package pers.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
	public static void main(String[] args)throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		DataSource ds = ctx.getBean("dataSource",DataSource.class);
		Connection conn = ds.getConnection();
		PreparedStatement pstmt = conn.prepareStatement("select * from hehe");
		ResultSet resultset = pstmt.executeQuery();
		
		while(resultset.next()) {
			System.out.println(1);
			System.out.println(resultset.getString("zhangsan"));			
		}
		
		//pstmt.executeUpdate();
		if(pstmt != null) {
			pstmt.close();
		}
		if(conn != null) {
			conn.close();
		}
	}
}
package pers.spring;

public class Person {
	public Person() {
		System.out.println("正在执行无参数的构造器!");
	}
	public void settest(String str) {
		System.out.println("配置文件传入的参数为:" + str);
	}
}


	
		
		
		
		
		
		
		
	
  


         
   4.0.0
   com.yiibai
   SpringJDBCTutorial
   0.0.1-SNAPSHOT

       
   
       1.7
       UTF-8
       UTF-8

   

   
       
       
           codelds
           https://code.lds.org/nexus/content/groups/main-repo
       
   

   

       
       
       
           org.springframework
           spring-core
           4.1.4.RELEASE
       

       
       
           org.springframework
           spring-context
           4.1.4.RELEASE
       

       
       
           org.springframework
           spring-jdbc
           4.1.4.RELEASE
       

       
       
       

       
       
           com.oracle
           ojdbc6
           11.2.0.3
       

       
       
       
           net.sourceforge.jtds
           jtds
           1.3.1
       

   

目录结构:

基于Maven的Spring 连接c3p0实例_第1张图片

你可能感兴趣的:(基于Maven的Spring 连接c3p0实例)