jdbc
获取连接
文件目录:
代码:jdbc.properties
jdbc.user=rootjdbc.password=439901jdbc.driverClass=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/examinitialPoolSize = 5maxPoolSize = 20applicationContext.
<? version="1.0" encoding="UTF-8"?><beans ns="http://www.spring work.org/schema/beans" ns:xsi="http://www.w3.org/2001/ Schema-instance" ns:context="http://www.spring work.org/schema/context" ns:aop="http://www.spring work.org/schema/aop" xsi:schemaLocation="http://www.spring work.org/schema/beans http://www.spring work.org/schema/beans/spring-beans.xsd http://www.spring work.org/schema/context http://www.spring work.org/schema/context/spring-context-4.0.xsd http://www.spring work.org/schema/aop http://www.spring work.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.spring work.context.ApplicationContext;import org.spring work.context.support.ClassPath ApplicationContext;public class Test { public static void main(String[] args) throws SQLException { ApplicationContext ac = new ClassPath ApplicationContext("applicationContext. "); 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.spring work.context.ApplicationContext;import org.spring work.context.support.ClassPath ApplicationContext;import org.spring work.jdbc.core.JdbcTemplate;public class JdbcTemplateTest { ApplicationContext ac = null; //修改代码 JdbcTemplate template = null; //修改代码 { ac = new ClassPath ApplicationContext("applicationContext. "); //修改代码 template = ac.getBean(JdbcTemplate.class); //修改代码 } @Test public void testInsert() { //修改代码 String sql = "insert into user values(null,?,?,?,?)"; [] s = {"老王","123","12315","12315@163.com"}; template.update(sql, s); //修改代码 }}修改applicationContext.
<? version="1.0" encoding="UTF-8"?><beans ns="http://www.spring work.org/schema/beans" ns:xsi="http://www.w3.org/2001/ Schema-instance" ns:context="http://www.spring work.org/schema/context" ns:aop="http://www.spring work.org/schema/aop" xsi:schemaLocation="http://www.spring work.org/schema/beans http://www.spring work.org/schema/beans/spring-beans.xsd http://www.spring work.org/schema/context http://www.spring work.org/schema/context/spring-context-4.0.xsd http://www.spring work.org/schema/aop http://www.spring work.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.spring work.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> //修改代码 </bean> </beans>输出:成功把数据导入数据库
继续阅读与本文标签相同的文章
上一篇 :
初识 Spring(02)---(IOC)
下一篇 :
正则表达式不再难懂
-
C#中用WMI实现对驱动的查询
2026-05-26栏目: 教程
-
WMI_COM_API
2026-05-26栏目: 教程
-
C#窗体实现打开关闭VM虚拟机
2026-05-26栏目: 教程
-
C#控制台打开VM虚拟机
2026-05-26栏目: 教程
-
C语言调用VIX_API开关虚拟机
2026-05-26栏目: 教程
