初识 Spring(08)---(jdbc)
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>
输出:成功把数据导入数据库

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
不设外键:用程序来实现表之间的关联
Association 相信有很多同学都有使用过Hibernate框架的开发经验,尽管现在开发人员对Hibernate的褒贬不一,我们暂且不谈这个话题。 图中的三个注解,正好解析了表与表之间的关联关系。我也就不这这方面进行过多的赘述了。所以... 今天的主题是:我想在不涉及表与表之间的关联关系的情况下对表进行关联。 换成对应的表的意思就是,不设外键,实现多对多关联。话不多说 先上一张我自己的设计手稿: 手稿 数据库:(数据库的编码(utf8)与数据库引擎(InnonDB)已经设置好了) CREATE table t_user( uid int(10) NOT NULL PRIMARY KEY AUTO_INCREMENT, username VARCHAR(100) not null, password VARCHAR(100) not null, createtime date not null )default CHARSET=utf8; -- 创建角色表 CREATE TABLE t_role( rid int(10) not null PRIMARY KEY AUTO_INCR...
-
下一篇
redis系列:通过文章点赞排名案例学习sortedset命令
前言 这一篇文章将讲述Redis中的sortedset类型命令,同样也是通过demo来讲述,其他部分这里就不在赘述了。 项目Github地址:https://github.com/rainbowda/learnWay/tree/master/learnRedis/case-sortedset 案例 demo功能是文章点赞排名等等,整个demo的大致页面如下。 准备工作 首先定义一个存储文章的key private static final String ZSET_KEY = "articleList"; redis操作对象 private RedisTemplate redisTemplate; //string 命令操作对象 private ValueOperations valueOperations; //zset 命令操作对象 private ZSetOperations zSetOperations; sortedset在Redis中的结构可以看下图(图片来源于Redis in Action)。 列表查询 @RequestMapping(value = "/getList/{...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- MySQL数据库在高并发下的优化方案
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS8编译安装MySQL8.0.19
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- MySQL8.0.19开启GTID主从同步CentOS8
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Docker容器配置,解决镜像无法拉取问题