java project使用spring jdbctemplate
程序员文章站
2022-04-25 10:00:31
...
package com.inzy.framework.tools;
import java.sql.Types;
import java.util.Date;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class SpringJdbcTemplate {
private static final String driverClassName = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/db";
private static final String dbUsername = "root";
private static final String dbPassword = "password";
private static DataSource dataSource;
public static JdbcTemplate getJdbcTemplate(){
dataSource = getDataSource();
JdbcTemplate template = new JdbcTemplate(dataSource);
return template;
}
public static DriverManagerDataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
return dataSource;
}
}
import java.sql.Types;
import java.util.Date;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class SpringJdbcTemplate {
private static final String driverClassName = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/db";
private static final String dbUsername = "root";
private static final String dbPassword = "password";
private static DataSource dataSource;
public static JdbcTemplate getJdbcTemplate(){
dataSource = getDataSource();
JdbcTemplate template = new JdbcTemplate(dataSource);
return template;
}
public static DriverManagerDataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
return dataSource;
}
}
推荐阅读
-
java 使用memcached以及spring 配置memcached完整实例代码
-
Spring JdbcTemplate之使用详解
-
【⭐】Java—Spring-—数据库操作—使用内置连接池,报读取不到驱动错误。Could not load JDBC driver class。
-
解决Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题
-
Spring 自动装配,Java 配置,XML配置、混合使用
-
使用spring cloud测试负载均衡时报java.lang.IllegalStateException: Request URI does not contain a valid hostname
-
【8】使用Java的方式配置Spring
-
Spring入门07--使用JAVA的方式配置Spring
-
spring-JdbcTemplate使用介绍
-
Java开发中Spring IOC容器的使用详解