java - 使用 spring boot 连接 DB2,不创建连接字符串
问题描述
与 db2 的连接。我看不到来自连接的任何消息,
当我执行 Gradle Run 时,不要显示错误消息,只是邮递员会像 JsonResponse(下图)一样获得“转移”。
我有下一个连接代码(值取自 application.properties):
public class ConnectionSingle {
@Autowired
private Environment env;
private Connection connection = null;
private static ConnectionSingle instance = null;
private ConnectionSingle() {}
public static synchronized ConnectionSingle getInstance() {
if (instance == null) {
instance = new ConnectionSingle();
}
return instance;
}
public Connection getConnection() throws SQLException {
if (connection==null) {
DriverManager.registerDriver(new AS400JDBCDriver());
connection = DriverManager.getConnection(
env.getProperty("database.url"),
env.getProperty("database.user"),
env.getProperty("database.password")
);
}
return connection;
}
}
而应用程序中的这段代码:
public class ApiController {
@RequestMapping(value="/irs/transfer", method=RequestMethod.POST)
public JsonResponse transfer(String sql) throws SQLException {
//Connection conexion =
ConnectionSingle.getInstance().getConnection();
// Declare the JDBC objects.
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection =
ConnectionSingle.getInstance().getConnection();
String selectSql = "SELECT * FROM LIBT91.EMPEQPI0
WHERE IDNO1 = 'TR-14638'";
statement = connection.createStatement();
resultSet = statement.executeQuery(selectSql);
System.out.println(selectSql);
// Print results from select statement
while (resultSet.next())
{
System.out.println(resultSet.getString(2) + " "
+ resultSet.getString(3));
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Close the connections after the data has been
handled.
if (resultSet != null) try { resultSet.close(); }
catch(Exception e) {}
if (statement != null) try { statement.close(); }
catch(Exception e) {}
if (connection != null) try { connection.close(); }
catch(Exception e) {}
}
return new JsonResponse("transfer");
}
应用程序属性
database.url=127.0.0.1
database.user=root
database.password=123456
gradle.build(依赖)
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-
plugin:2.0.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
mainClassName = 'dbsapi.Application'
bootJar {
baseName = 'dbs-api'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("net.sf.jt400:jt400:9.5")
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('com.jayway.jsonpath:json-path')
}
解决方案
推荐阅读
- javascript - 标记一个元素时验证所需的复选框
- regex - 正则表达式在bash中的值和键周围添加双引号
- azure - Dynamics CRM 365 - 使用虚拟实体
- java - 我有一个由多个 json 元素组成的字符串,而这些元素又由多个元素组成,如何将其转换为 json 对象
- swift - 要求协议实现者用@objc 标记函数
- python - jupyter notebook 找不到肯定安装的 python 模块
- java - 检查文件是否存在于嵌套的不均匀目录中?
- python - 家庭网络中python中的DLNA服务器
- json - Vscode在使用变量之前转义斜杠
- mongodb - 带有卷的docker中的mongo db持久性