欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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;
    }
}
相关标签: jdbctemplate