创建JDBC.properties,里面写连接数据库需要的4个值
jdbc.user=root
jdbc.password=123456
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/lhw
applicationContext中的配置
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
</bean>
简单的查询数据
public void test() throws SQLException { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); DataSource dataSource = ac.getBean(DataSource.class); Connection con = dataSource.getConnection(); String sql="select * from student"; PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while(rs.next()){ String name = rs.getString("name"); String school = rs.getString("school"); double score = rs.getDouble("score"); System.out.println(name+school+score); } }