开始新工作,需要学习Java(十多年来一直是.NET开发人员)。来自更高层次的授权要求所有新的东西都要在Java / Oracle中完成。
所以我正在进行PluralSight培训,现在我正在努力学习JDBC的复杂性。
我在Oracle数据库中有下表(对我来说也是一种新技术)。
CREATE TABLE "TEST"."ACCOUNT"
(
"ACCOUNT_ID" NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY VALUE,
"ACCOUNT_NAME" VARCHAR2(20)
"ACCOUNT_NUMBER" VARCHAR2(20)
"ACCOUNT_TYPE" VARCHAR2(20)
)
我使用Repository模式设置了一些代码。但这很简单:
public static void main(String[] args) throws IOException {
IAccountMapper accountMapper = new AccountMapper();
IConfiguration configuration = new OracleConfiguration();
try (
IDataAccess<ResultSet> dataAccess = new OracleDataAccess(configuration);
IAccountRepository accountRepo = new AccountRepository(accountMapper, dataAccess);
){
//nothing here
List<Account> accounts = accountRepo.query(new AllAccountsSpecification());
} catch (Exception e){
System.err.println(e.getMessage());
}
}
IDataAccess界面:
public interface IDataAccess<T> {
T get(String query) throws SQLException;
}
我的所有实现都在基类中,并且引用了子类 - 即:
public interface IOracleDataAccess extends IDataAccess<ResultSet>{
}
public class OracleDataAccess extends DataAccessBase<ResultSet> implements IOracleDataAccess {
public OracleDataAccess(IConfiguration configuration) throws SQLException {
super(configuration);
}
IDataAccess的实现(一个基类,其中T是回购使用的实体):
public abstract class DataAccessBase<T> implements AutoCloseable {
protected final IConfiguration configuration;
protected Connection connection;
protected PreparedStatement statement;
protected ResultSet resultSet;
public DataAccessBase(IConfiguration configuration) {
this.configuration = configuration;
this.connection = DriverManager.getConnection(configuration.getConnectionString(),
configuration.getUsername(), configuration.getPassword());
}
public T get(String query) throws SQLException {
statement = connection.prepareStatement(query);
return (T)statement.executeQuery();
}
}
此DataAccess类被注入到我的Repository类中,用于返回结果:
public abstract class ReadOnlyRepositoryBase<T> implements IReadOnlyRepository<T> {
protected final IMapper<ResultSet, T> mapper;
protected final IDataAccess<ResultSet> database;
public ReadOnlyRepositoryBase(IMapper<ResultSet, T> mapper, IDataAccess<ResultSet> database) {
this.mapper = mapper;
this.database = database;
}
public List<T> query(ISpecification specification){
List<T> entities = new ArrayList<>;
try {
System.out.println(specification.toSqlQuery());
//This is what is output:
//SELECT * FROM ACCOUNT
//this returns 1 record when: "select tablespace_name, table_name from user_tables"
//returns no records when "select * from account"
ResultSet rs = database.get(specification.toSqlQuery());
//loop is never entered for "select * from account"
//runs once for "select tablespace_name, table_name from user_tables"
while(rs.next()){
entities.add(mapper.map(rs));
}
} catch (SQLException sqlEx){
sqlEx.printStackTrace();
}
return entities;
}
}
问题是什么都没有回来(ResultSet next()方法返回false)这个SQL:
SELECT * FROM ACCOUNT
从Oracle SQL Developer IDE返回2条记录 - 但代码中没有任何记录。
如果我从代码运行此查询:
"SELECT TABLESPACE_NAME, TABLE_NAME FROM USER_TABLES"
我得到1条记录(TEST,ACCOUNT)
我是否需要进一步限定SQL中的表以获取记录?
我使用与之连接的相同凭据登录数据库 - 因此我认为这不是权限。
答案 0 :(得分:0)
感觉像个白痴 - 但我想我不是第一个在这里。 @AlexPoole是当场的。我已经通过IDE插入了记录 - 但显然 我没有承诺。来自SQL Server - 我不需要在
之后提交Insert into [Table] values (stuff)
我想这与Oracle有所不同。