gpt4 book ai didi

java - 如何在类中使用方法或执行语句

转载 作者:行者123 更新时间:2023-12-01 17:43:14 27 4
gpt4 key购买 nike

我不知道我是否需要更多咖啡,或者我的头很累,但是,我感觉自己像个白痴:)

我做错了什么???

我想调用类中的方法,但出现编译错误。

Compilation failure: Compilation failure:
java:[35,1] error: illegal start of type
java:[35,21] error: <identifier> expected
java:[35,22] error: ';' expected

我无法在类中执行此 block

//this is what doesn't work         
if (custkey.contains(",")) {
String[] ck = custkey.split(",");
for ( String k : ck) {
this.loadRefDb(k);
} else {
this.loadRefDb(custkey);
}
}

我的类(class)

package com.ge.digital.fleet.dataservice.impl.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;

import com.ge.digital.fleet.dataservice.impl.db.RefDatabase;

public class RefReplicatedDataProcessor {

private static final Logger log = LoggerFactory.getLogger(RefReplicatedDataProcessor.class);

private RefDatabase refDb = null;
private DataSource dataSource;
private String custkey;

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setCustkey(String custkey) {
this.custkey = custkey;
}
public void setRefDatabase(RefDatabase refDb) {
this.refDb = refDb;
}
//this is what doesn't work
if (custkey.contains(",")) {
String[] ck = custkey.split(",");
for ( String k : ck) {
this.loadRefDb(k);
} else {
this.loadRefDb(custkey);
}
}

public void loadRefDb(String custkey) throws SQLException {

log.info("Reference Replicated Data Processor :: start");

refDb.dropDb();
setAssociations(custkey);
refDb.replicationComplete();

log.info("Reference Replicated Data Processor :: Finish");

}

/***
* name: setAssociations(custkey)
* Loads/Builds the cache database with values found in mysql database
*
* returns a List of associations
* G1.DWATT,112-A-001_Gas_Turbine
* G1.ATID,112-A-001_Gas_Turbine
* G1.dvar, 112-A-001_Gas_Turbine
* ...
*/
public void setAssociations(String custkey) throws SQLException {

String reference = "";
String asset = "";
String dbname = "iprcmt1.fleet_associations"; //from old impl database - TODO new database impl

String query = "select reference, asset from " + dbname + " where custkey = ?";
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, custkey);
try (ResultSet rs = stmt.executeQuery()) {
if (! rs.next() ) {
log.info("SQL Warning ! No associations for key: " + custkey);
} else {
do {
reference = rs.getString(1);
asset = rs.getString(2);
log.info("SQL Associations reference: " + reference + " and asset: " + asset);
refDb.addRow(reference, asset);
} while (rs.next());
}
} catch (Exception ex) {
log.error("SQL Cannot Execute ResultSet Query!");
log.error(ex.getMessage());
}
} catch (Exception e) {
log.error("SQL Cannot Create DataSource Connection! Cannot Create Prepared Statement!");
log.error(e.getMessage());
}
}

}

最佳答案

也许是这个? :)

if (custkey.contains(",")) {
String[] ck = custkey.split(",");
for ( String k : ck) {
this.loadRefDb(k);
}
}else {
this.loadRefDb(custkey);
}

关于java - 如何在类中使用方法或执行语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58329222/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com