初识 Spring(08)---(jdbc)

简介: jdbc获取连接文件目录:代码:jdbc.propertiesjdbc.user=rootjdbc.

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>

输出:成功把数据导入数据库   

 

目录
相关文章
|
4月前
|
druid Java 数据库连接
SpringBoot原理分析 | Spring Data整合:JDBC、Druid、Mybatis
SpringBoot原理分析 | Spring Data整合:JDBC、Druid、Mybatis
62 0
|
3月前
|
Java 数据库连接 数据库
Spring4.X系列之Spring JDBC
Spring4.X系列之Spring JDBC
24 0
|
3月前
|
前端开发 Java Maven
Spring-Spring MVC + Spring JDBC + Spring Transaction + Maven 构建web登录模块
Spring-Spring MVC + Spring JDBC + Spring Transaction + Maven 构建web登录模块
60 0
|
4月前
|
XML Java 数据库连接
【Spring】JDBC、AOP、事务
【Spring】JDBC、AOP、事务
|
9月前
|
SQL Java 数据库连接
java原生jdbc到spring的jdbcTemplate
java原生jdbc到spring的jdbcTemplate
|
6月前
|
XML Java 数据库连接
使用Spring JDBC中的JdbcTemplate对数据进行增删改查操作教程~
使用Spring JDBC中的JdbcTemplate对数据进行增删改查操作教程~
|
7月前
|
Java 数据库连接 API
Spring中如何操作JDBC
Spring中如何操作JDBC
|
10月前
|
SQL Java 关系型数据库
数据库|Spring整合JDBC
数据库|Spring整合JDBC
113 0
|
10月前
java202304java学习笔记第六十天-ssm-spring配置文件-抽取jdbc.properties文件
java202304java学习笔记第六十天-ssm-spring配置文件-抽取jdbc.properties文件
36 0
|
11月前
|
SQL 存储 Java
Spring JDBC-NamedParameterJdbcTemplate模板类
Spring JDBC-NamedParameterJdbcTemplate模板类
47 0

热门文章

最新文章