文章目录
目录结构
.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 使用外部属性文件 -->
<context:property-placeholder location="classpath:resource/vedio/beans-jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
</beans>
.properties
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/jt_db?characterEncoding=utf8
user=root
password=root
Test 代码
package spring5.vedio.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class Main {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("resource/vedio/beans-properties.xml");
ComboPooledDataSource pool = ac.getBean("dataSource" , ComboPooledDataSource.class) ;
Connection conn = null ;
PreparedStatement ps = null ;
ResultSet rs = null ;
try {
conn = pool.getConnection();
System.out.println("-- 获得连接 :" + conn);
String sql = "select * from stu ; " ;
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
System.out.println("-- 打印查询结果 --");
show(rs);
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn , ps , rs ) ;
}
}
private static void show(ResultSet rs) {
ResultSetMetaData metaData = null ;
StringBuilder sb = new StringBuilder("[ ");
String s = ", " ;
try {
metaData = rs.getMetaData();
int col = metaData.getColumnCount();
while(rs.next()) {
for (int i = 1; i <= col; i++) {
Object obj = rs.getObject(i);
sb.append(obj) ;
sb.append(s);
}
sb.delete(sb.length() - s.length(), sb.length()) ;
sb.append("]") ;
System.out.println(sb.toString());
sb.delete(2, sb.length()) ;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn = null;
}
}
if(ps!=null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ps = null ;
}
}
if(rs != null ) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
rs = null ;
}
}
}
}