标签:getbean use name min etc roo source root app
方法一(推荐):使用xml文件连接
xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK</property> <property name="user">root</property> <property name="password"></property> </default-config> </c3p0-config>
类文件
public static void main(String[] args) throws Exception{ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); DataSource ds = (DataSource) context.getBean("dataSource"); Connection conn = ds.getConnection(); conn.close(); }
方法二:直接在类文件中连接数据库
public static void main00(String[] args) throws Exception { ComboPooledDataSource ds = new ComboPooledDataSource(); \\依次设置连接数据库的各项属性 ds.setDriverClass("com.mysql.jdbc.Driver"); ds.setJdbcUrl("jdbc:mysql://localhost:3306/mydb"); ds.setUser("root"); ds.setPassword(""); ds.setMinPoolSize(5); ds.setMaxPoolSize(20); Connection conn = ds.getConnection(); conn.close(); }
标签:getbean use name min etc roo source root app
原文地址:http://www.cnblogs.com/jonsnow/p/6641001.html