jdbc
获取连接
文件目录:
代码:jdbc.properties
jdbc.user=root
jdbc.password=439901
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/exam
initialPoolSize = 5
maxPoolSize = 20
applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="initialPoolSize" value="${initialPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
</beans>
Test.java
package com.neuedu.test;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws SQLException {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = ac.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}
}
输出:成功获取连接
修改代码:(语句块)
新建 Person.java
package com.neuedu.test;
public class Person {
private String name = "zhangsan";
private int age;
public Person() {
name = "wangwu";
System.out.println("Persom(){}");
}
public Person(String name) {
this.name = name;
}
public Person(String name,int age) {
this.name = name;
this.age = age;
}
{
name = "lisi";
System.out.println("创建Person类的对象要初始化一些操作");
}
}
新建JdbcTemplateTest.java
package com.neuedu.test;
import static org.junit.Assert.*;
import org.junit.Test;
public class JdbcTemplateTest {
@Test
public void test() {
Person p = new Person();
}
}
输出:
修改代码:(链接数据库,传递数据)
数据库:
代码:
修改JdbcTemplateTest.java
package com.neuedu.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcTemplateTest {
ApplicationContext ac = null; //修改代码
JdbcTemplate template = null; //修改代码
{
ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //修改代码
template = ac.getBean(JdbcTemplate.class); //修改代码
}
@Test
public void testInsert() { //修改代码
String sql = "insert into user values(null,?,?,?,?)";
Object[] objects = {"老王","123","12315","12315@163.com"};
template.update(sql, objects); //修改代码
}
}
修改applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="initialPoolSize" value="${initialPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property> //修改代码
</bean>
</beans>
输出:成功把数据导入数据库