Retrieve sum from database

       try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");        
            String url = "jdbc:sqlserver://localhost;databaseName=QLLTCK;integratedSecurity=true;";
            Connection connection = DriverManager.getConnection(url);
            String sql = "SELECT SUM(soSV) AS sumSoSV FROM PHANCONG WHERE tenMH = 'Công nghệ phần mềm chuyên sâu'";
            PreparedStatement pst = connection.prepareStatement(sql); 
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                int SumSoSV = rs.getInt("sumSoSV");
                JOptionPane.showMessageDialog(null,"Số sinh viên dự thi: " + SumSoSV);
            }
        }
       catch(Exception sqlex){
            JOptionPane.showMessageDialog(null, sqlex);
        }

I cannot retrieve the sum of soSV from my PHANCONG table. Its result is 0. When I tried to use String instead of int, its result is null. Can someone help me? Thanks.

Image

Upvotes: 1

Views: 53

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 175716

I guess you need to add N before string literal to handle unicode characters:

String sql = "SELECT SUM(soSV) AS sumSoSV FROM PHANCONG WHERE tenMH = N'Công nghệ phần mềm chuyên sâu'"

Upvotes: 2

Related Questions