我遇到此错误:java.sql.SQLException:Java应用程序中的列名无效

时间:2019-05-31 06:08:13

标签: java sql oracle

我构建了一个应用程序,该应用程序在一个表中计算一些税表,并且必须在另一个表中插入数字。 当我在第一个表中计算税收表格时,我使用此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:无效的列名

为什么会这样?

我该怎么做才能使其正常工作?

我尝试用大写字母写count(distingt 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);}
 }

}

我想获取查询结果:从dec_declaratii中选择count(distinct cui),其中id> 142321849 and tip_declaratie ='D212'and anul_duk> = 2019 and cod_stare_prelucrare_intern in('DUK_VLD','GEN_MSJ')“ ;在Java变量中,它给了我这个错误:java.sql.SQLException:无效的列名

1 个答案:

答案 0 :(得分:0)

使用别名命名计数列

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')";