首页 > 解决方案 > 使用 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')
}

标签: javaspring-bootdb2

解决方案


推荐阅读