您现在的位置是:首页 > 文章详情

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

日期:2018-08-07点击:325

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> 

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

 

原文链接:https://yq.aliyun.com/articles/623903
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章