java - 我有这个错误:java.sql.SQLException:Java 应用程序中的列名无效
问题描述
我构建了一个应用程序来计算表格中的一些税表,并且必须在另一个表格中插入数字。当我计算第一个表中的税表时,我使用以下 sql 查询:
select count(distinct cui) from dec_declaratii where id > 142321849 and
tip_declaratie='D212' and anul_duk>=2019 and cod_stare_prelucrare_intern
in ('DUK_VLD', 'GEN_MSJ')";
当我从 Oracle Toad 运行它时它可以工作,但是当我把它放在 java 中并尝试使用 rs.getString("count(distinct cui)") 得到结果时,它给了我这个错误:java.sql.SQLException: Invalid column姓名
为什么会这样?
我该怎么做才能让它发挥作用?
我尝试用大写字母写 count(distinct cui),如果我写 count(*),它可以工作,但我需要不同数量的税表。
public class Baza {
int idmin = 142321849;
int rezultat = 391320;
String host = "xxx";
String user = "xxx";
String pass = "xxx";
String user2 = "xxx";
String pass2 = "xxx";
String host2 = "xxx";
String nr = "0";
public void conectare() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(host, user, pass);
Statement st = con.createStatement();
String sql = "select count(distinct cui) from dec_declaratii where id
> 142321849 and tip_declaratie='D212' and anul_duk>=2019 and
cod_stare_prelucrare_intern in ('DUK_VLD', 'GEN_MSJ')";
ResultSet rs = st.executeQuery(sql);
while(rs.next()) {
nr = rs.getString("count(DISTINCT CUI)");
System.out.println(nr);
}
}catch(Exception e) {System.out.println(e);}
int nr2 = Integer.parseInt(nr);
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(host2, user2, pass2);
Statement st = con.createStatement();
String sql = "update nr_tot_dec_212 set nr_dec='"+nr+"', data=sysdate
where id=1";
st.executeUpdate(sql);
}catch(Exception e) {System.out.println(e);}
}
}
我想得到查询的结果: select count(distinct cui) from dec_declaratii where id > 142321849 and tip_declaratie='D212' and anul_duk>=2019 and cod_stare_prelucrare_intern in ('DUK_VLD', 'GEN_MSJ')"; 在Java 变量。它给了我这个错误:java.sql.SQLException: Invalid column name
解决方案
使用别名命名您的计数列
select count(distinct cui) as cnt from dec_declaratii where id > 142321849 and
tip_declaratie='D212' and anul_duk>=2019 and cod_stare_prelucrare_intern
in ('DUK_VLD', 'GEN_MSJ')";
推荐阅读
- r - 使用 conda 安装 R 3.6.1 无限期挂起
- angular - 类型参数不可分配给“OperatorFunction”类型的参数
' - python - 将python应用程序部署到Azure Web App时为tensorflow获取ReadTimeoutError
- image - 将图像与响应式文本引导程序对齐
- pip - 带有 openssl 1.1 的 gdal 破坏了 pip 安装和其他依赖于 macos 上的 openssl 1.0 的东西
- java - JLabel 直到循环结束才更新
- javascript - D3 单击附加到线的符号
- java - Java 初学者:使用循环将输入值分成两半,但不能使用数组、内置排序例程或任何其他 Java 集合类
- python - 在 PyCharm 中使用 requests 和 BeautifulSoup 后没有输出
- node.js - nodejs 导入模块没有按预期工作,