spring jdbc 配置数据源连接数据库
概述
在XML配置数据源,并将数据源注册到JDBC模板
JDBC模板关联业务增删改查
在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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.223.129:3358/vodb?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai"/>
<property name="username" value="automng"/>
<property name="password" value="Automng_123"/>
</bean>
<bean id="studentJDBCTemplate"
class="data.model.StudentJDBCTemplate">
<property name="dataSource" ref="dataSource" />
</bean> </beans>
JDBC模板关联业务增删改查
package data.model; import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate; public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource;
private JdbcTemplate jdbcTemplateObject; public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplateObject = new JdbcTemplate(dataSource);
} public void create(String name, Integer age) {
String SQL = "insert into Student (name, age) values (?, ?)";
jdbcTemplateObject.update( SQL, name, age);
System.out.println("Created Record Name = " + name + " Age = " + age);
return;
} public Student getStudent(Integer id) {
String SQL = "select * from Student where id = ?";
Student student = jdbcTemplateObject.queryForObject(SQL,
new Object[]{id}, new StudentMapper());
return student;
} public List<Student> listStudents() {
String SQL = "select * from Student";
List <Student> students = jdbcTemplateObject.query(SQL,
new StudentMapper());
return students;
} public void delete(Integer id){
String SQL = "delete from Student where id = ?";
jdbcTemplateObject.update(SQL, id);
System.out.println("Deleted Record with ID = " + id );
return;
} public void update(Integer id, Integer age){
String SQL = "update Student set age = ? where id = ?";
jdbcTemplateObject.update(SQL, age, id);
System.out.println("Updated Record with ID = " + id );
return;
} }
遇到问题
Client does not support authentication protocol requested by server; consider upgrading MySQL client
最新的mysql模块并未完全支持MySQL 8的“caching_sha2_password”加密方式,而“caching_sha2_password”在MySQL 8中是默认的加密方式。因此,下面的方式命令是默认已经使用了“caching_sha2_password”加密方式,该账号、密码无法在mysql模块中使用。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/vodb?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
如果数据库版本是mysql5.7,那么需要验证
应用服务器到数据库的网络是否连通,是否有限制
用户名密码以及端口是否正确
spring jdbc 配置数据源连接数据库的更多相关文章
- Spring JDBC配置数据源
在本系列教程中,使用的的是MySQL数据库,并创建一个数据库实例:test,在这个数据库实例:test中创建一个表student.如果您使用任何其他数据库,则可以相应地更改DDL和SQL查询,这问题不 ...
- spring中配置数据源
spring中配置数据源的几种常见方式: #mysql 数据库配置(jdbc.properties) jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.u ...
- 【Spring JDBC】数据源配置(二)
一.Spring内置数据源 1. 创建Maven Project,修改pom.xml <properties> <!-- JDK版本 --> <java.version& ...
- Spring中配置数据源的4种形式(转)
原文http://blog.csdn.net/orclight/article/details/8616103 不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的 ...
- 搭建spring工程配置数据源连接池
Spring作为一个优秀的开源框架,越来越为大家所熟知,前段时间用搭了个spring工程来管理数据库连接池,没有借助Eclipse纯手工搭建,网上此类文章不多,这里给大家分享一下,也作为一个手记. 工 ...
- spring boot 配置数据源
以postgreSQL为例,方便下次直接使用. 其中pom.xml引入如下依赖. <?xml version="1.0" encoding="UTF-8" ...
- Spring中配置数据源的4种形式
不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的数据源配置方式: spring自带的数据源(DriverManagerDataSource),DBCP数据源,C3P0数据源 ...
- Spring中配置数据源的四种方式
1.spring自带的数据源 <bean id="dataSource" class="org.springframework.jdbc.datasource.Dr ...
- Spring中配置数据源常用的形式
不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的数据源配置方式: spring自带的数据源(DriverManagerDataSource),DBCP数据源,C3P0数据源 ...
随机推荐
- Allure 生成测试报表
Allure官方文档参考地址:https://docs.qameta.io/allure/#_testng 1.在maven中添加依赖并进行相应的配置: <!-- 实现版本控制 --> & ...
- sqlalchemy insert or ignore
insert ignore # insert ignoreinsert_stmt = TimePoint.__table__.insert().prefix_with(" ignore&qu ...
- macos proxy_bypass_macosx_sysconf exception
macos, 在rpc调用request请求时,在proxy_bypass_macosx_sysconf 无法返回 解决方法: import requests session = requests.S ...
- Spring中bean的初始化和销毁几种实现方式
Bean的生命周期 : 创建bean对象 – 属性赋值 – 初始化方法调用前的操作 – 初始化方法 – 初始化方法调用后的操作 – --- 销毁前操作 – 销毁方法的调用. [1]init-metho ...
- Mac 下 Nginx 配置使用
安装 homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/m ...
- 菜鸡的Java笔记 第二十一 final 关键字
使用final定义类,属性,方法 final在一些书中被称为终结器,意思是:利用final定义的类不能够有子类,利用final定义的方法不能够被覆写,利用final定义的变量就成 ...
- Serverless 下的微服务实践
作者:弈川 审核&校对:筱姜.潇航 编辑&排版:雯燕 微服务架构介绍 微服务架构诞生背景 在互联网早期即 Web 1.0 的时代,当时流行的是单体应用,研发团队比较小,主要是外部网页, ...
- kali 安装typora
一.安装 官网下载文件解压,并移动到 /opt 文件夹下 二.赋权 在typora目录的bin文件夹下执行命令 ./typora 会报错[7442:0707/173355.682906:FATAL:s ...
- Linux 软连接与硬连接 区别
先说结论 软连接相当于快捷方式,访问软连接会被替换为其指向的绝对路径,如果其指向的文件被删除,则无法访问. 硬连接相当于指针,与它指向的文件都指向相同的inode,当其指向的文件被删除,inode由于 ...
- [cf566C]Logistical Questions
记$d(x,y)$为$x$到$y$的距离,$cost_{x}=\sum_{i=1}^{n}w_{i}d(x,i)^{\frac{3}{2}}$为$x$的代价 取$C$为足够大量,对于一条边权为$w$的 ...