java创建connection,使用JDBC和Spring Boot创建java.sql.Connection

I wanted to create two static functions: java.sql.Connection Connection.getConnection() and void Connection.closeConection() to obtain and terminate connections so that I can execute JDBC queries on my remote RDS instance. Here's what I wrote:

application.properties

spring.datasource.url=jdbc:mysql://myRDSEndpoint:3306/mySchemaName

spring.datasource.username=myUsername

spring.datasource.password=myPassword

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

RDSConnection.java:

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

@Component

public class RDSConnection {

public static String driver;

public static String url;

public static String user;

public static String pass;

@Autowired

public RDSConnection getRDSConnection(@Value("${spring.datasource.driver-class-name}") String driver,

@Value("${spring.datasource.url}") String url,

@Value("${spring.datasource.username}") String user,

@Value("${spring.datasource.password}") String pass) {

RDSConnection.driver = driver;

RDSConnection.url = url;

RDSConnection.user = user;

RDSConnection.pass = pass;

return this;

}

}

Connection.java

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import java.sql.DriverManager;

import java.sql.SQLException;

@Component

@Slf4j

public class Connection {

private static java.sql.Connection sqlConnection;

public static java.sql.Connection getConnection() {

String driver = RDSConnection.driver;

String url = RDSConnection.url;

String user = RDSConnection.user;

String pass = RDSConnection.pass;

if(sqlConnection != null) return sqlConnection;

try {

Class.forName(driver);

sqlConnection = DriverManager.getConnection(url, user, pass);

} catch (ClassNotFoundException | SQLException e) {

log.error(e.getMessage());

}

return sqlConnection;

}

public static void closeConnection(java.sql.Connection sqlConnection) {

try {

sqlConnection.close();

} catch (SQLException e) {

log.error(e.getMessage());

}

}

}

This works, but I was wondering if there is a better way to do this. Is there a way to make the static variables in RDSConection final as well, since I know that the spring.datasource.* values are not going to change? If so, how do I inject the values from application.properties?

解决方案

There are some things that can be improved...

First, I you recommend you using SPRING DATA, you can use EntityManager and get the connection with this:

@PersistenceContext

private EntityManager entityManager;

public void method() {

... entityManager.unwrap(Session.class) ...

}

For use spring data with spring boot, just use:

@SpringBootApplication

@EntityScan(basePackages = {

"com.your.project"

})

@EnableJpaRepositories(basePackages = {

"com.your.project"

})

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

Second, if you really want to use JDBC, I hope you use JAVA 7 / +

In this case, first use try-with-resources,

this will close the connection automatically.

Something like:

try (Connection sqlConnection = DriverManager.getConnection(url, user, pass);

PreparedStatement ps = sqlConnection.createPreparedStatement(sql);

ResultSet rs = ps.executeQuery()) {

// process the resultset here, all resources will be cleaned up

} catch (SQLException e) {

log.error(e.getMessage());

}

}

And Class.forName(driver); It is no longer necessary. In Java 6+ because DriverManager will auto-load drivers found on the classpath.

For more info: Why do I need to explicitly write Class.forName() when using JDK 8?

Finally, if you really do not want to use the Spring settings, you do not have a reason to use application.properties. You can use constants that will be static and final.

PS: Remember that you have to close all ResultSet and PreparedStatement, not just connection as a precaution.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值