【问题标题】:display 0 in the result for no row在结果中显示 0 表示没有行
【发布时间】:2013-03-14 15:43:38
【问题描述】:

我需要编写一个连接数据库的java程序,程序应该提示用户输入7位数字ID,然后计算每个字母等级出现的次数和总等级数。

例如:学生 12345 一个:1 乙:0 C'S :3 D'S : 0 F'S: 哦 总成绩:4

我下面的代码工作正常,但问题是 no row 的结果不会给我 0 .. e.x :它只给我 A、C 并删除其他人

所以你知道如何在结果中显示 0。

谢谢你

import java.sql.*; // Use classes in java.sql package
import java.util.*;

public class DDBB {

    public static void main (String args []) {
        int a;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a 7 digit Number for StudentID");
        a = in.nextInt();
        System.out.println("StudentID" +" "+ a);
        int countgrade ;
        String GRADE1;
        int totalcount;
        Connection conn =null;
        Statement st =null;
        try {
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            // Class.forName("oracle.jdbc.driver.OracleDriver");
            String url ="jdbc:oracle:thin ....";
            conn = DriverManager.getConnection(url,"user", "password");
            // create a Statement object
            PreparedStatement st1 = conn.prepareStatement(
                "SELECT GRADE, Count(*) AS COUNT1 FROM GRADING WHERE StudentID = ? GROUP BY Grade ORDER BY GRADE");
            st1.setInt(1, a);
            ResultSet S = st1.executeQuery();
            while (S.next()) {
                countgrade = S.getInt("COUNT1");
                GRADE1 = S.getString("GRADE");
                System.out.println(GRADE1 + "'s :"+countgrade+"\n");
            }
            String Q2 ="SELECT COUNT(GRADE) AS COUNT2 FROM GRADING WHERE StudentID = ? ";
            ResultSet S1 = st1.executeQuery(Q2);
            while (S1.next()) {
                totalcount = S1.getInt("COUNT2");
                System.out.println("Total grades:"+ " " + totalcount+"\n");
            }
            S.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                // close the Statement object using the close() method
                if (st != null) {
                    st.close();
                }
                // close the Connection object using the close() method
                if (conn != null) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

【问题讨论】:

  • 这需要一些格式化
  • @MajidLAISSI 你现在可以看问题了吗?
  • 对我来说听起来像是一道作业题。
  • @LuiggiMendoza 你告诉我 :)

标签: java sql jdbc plsql


【解决方案1】:

您似乎应该使用for 循环而不是while 来打印成绩(或者可能只是不使用循环)。

为什么不做这样的伪代码:

int A = B = C = D = F = 0;
/* Get results as you already are, stored in result set s */
while( s.next() )
{
    string grade = s.GetString("GRADE");
    if( grade == "A" )
        A = s.GetInt("COUNT1")
    if( grade == "B" )
        B = s.GetInt("Count1")
    /*etc*/
}

在while循环结束时,只输出“A's:” + A + “​​B's:” ...

当您显示最终成绩时,只需显示 A + B + C + D + F。

这将其缩减为一个 SQL 查询,并且始终显示每个字母的等级。

【讨论】:

  • 当然,有些地方可以收紧。我把它作为练习留给你 len - 我在上面输入的内容会起作用,但它可以更好地工作,或者通过一些工作可以更清晰地阅读。享受吧!
猜你喜欢
  • 2015-12-15
  • 2019-02-16
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 2019-10-08
  • 1970-01-01
相关资源
最近更新 更多