使用Spring Boot的连接DB2,不建立连接字符串

时间:2018-06-25 20:17:56

标签: 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");
        }  

application.properties

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')
}

0 个答案:

没有答案