jdbctemplate入门案例
Posted 阿布都日
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdbctemplate入门案例相关的知识,希望对你有一定的参考价值。
pom配置:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>jdbcTemplate</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> </dependency> </dependencies> <repositories> <repository> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>14</java.version> <maven.compiler.source>14</maven.compiler.source> <maven.compiler.target>14</maven.compiler.target> <encoding>UTF-8</encoding> </properties> </project>
代码:
package com.example.jdbctemplate; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; /** * JdbcTemplate最基本用法 */ public class JdbcTemplateDemo { public static void main(String[] args) { //准备spring内置数据源 DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName("org.h2.Driver"); ds.setUrl("jdbc:h2:file:~/.h2/h2"); ds.setUsername("root"); ds.setPassword("123456"); //创建jdbcTemplate对象 JdbcTemplate jt = new JdbcTemplate(); jt.setDataSource(ds); //执行操作 jt.execute("insert into account(name,money)values(\'ccc\',1000)"); } }
以上是关于jdbctemplate入门案例的主要内容,如果未能解决你的问题,请参考以下文章
spring boot与jdbcTemplate的整合案例2