0

I'm working on an exercise mixing mysql and java. I create a function to convert numerical number in roman number. I would like to do the same by calling mysql table in a database according to the needs. Here is my table:

numerical | roman |
+-----------+-------+
|      1000 | M     |
|       900 | CM    |
|       500 | D     |
|       400 | CD    |
|       100 | C     |
|        90 | XC    |
|        50 | L     |
|        40 | XL    |
|        10 | X     |
|         9 | IX    |
|         5 | V     |
|         4 | IV    |
|         1 | I    

In my java code, I need to go through all the table row by row. Get the result and compare to a number. For example 2536: 1000 -> row 1, row 2 nothing, then 500 -> row 3 ...

Here is my code:

import java.sql.*;
import java.util.Properties;
import java.util.Scanner;

public class MysqlTest
{
  // The JDBC Connector Class.
    

    static String to_roman_number(int number, ResultSet rs, Statement stmt, ResultSetMetaData rsmd) {

        // String[] roman_table = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        // int[] numerical_table = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String result = " ";
        int my_number = number;
    
                    
        for(int i=1; i < rsmd.getPrecision(1); i++){
            rs.absolute(i);
            String roman = rs.getString("roman");
            int numerical = rs.getInt("numerical");

                    
            if(number / numerical !=0 && number != 0){
                        
                int rec = number / numerical;
                result += roman.repeat(rec);
                number -= rec * numerical;
            }
            else{
                continue;
            }
        }
                    
        return Integer.toString(my_number) + " ->" + result;
    }

  public static void main(String[] args) throws
                             ClassNotFoundException,SQLException
  {
    // Class.forName(xxx) loads the jdbc classes and
    // creates a drivermanager class factory
    String dbClassName = "com.mysql.cj.jdbc.Driver";
    Class.forName(dbClassName);

    // Properties for user and password. Here the user and password are both 'paulr'
    /* */ Properties p = new Properties();
    p.put("user","user");
    p.put("password","password");

    String CONNECTION ="jdbc:mysql://localhost/romanumerals";
    String sql = "SELECT * FROM numbers";
    

    

    // Now try to connect
    try (Connection c = DriverManager.getConnection(CONNECTION,p);
    Statement stmt  = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE))
    {

    ResultSet rs = stmt.executeQuery(sql);
    ResultSetMetaData rsmd = rs.getMetaData();
    
    while(true){
            
            Scanner in = new Scanner(System.in);
            System.out.println("Please choose a number: ");
            int number = in.nextInt();

            if(number > 3000){
                System.out.println("Please choose a number smaller or equal to 3000");
            }
            else{
                System.out.println(to_roman_number(number, rs, stmt, rsmd));
            }
        
        }
        
        

    }catch (SQLException ex) {
        System.out.println(ex.getMessage());
    }

    System.out.println("It works !");
    // c.close();
    }
}

This code result in the following error:


MysqlTest.java:18: error: unreported exception SQLException; must be caught or declared to be thrown
        for(int i=1; i < rsmd.getPrecision(1); i++){
                                          ^
MysqlTest.java:19: error: unreported exception SQLException; must be caught or declared to be thrown
            rs.absolute(i);
                       ^
MysqlTest.java:20: error: unreported exception SQLException; must be caught or declared to be thrown
            String roman = rs.getString("roman");
                                       ^
MysqlTest.java:21: error: unreported exception SQLException; must be caught or declared to be thrown
            int numerical = rs.getInt("numerical");

I know I need to catch it in a SQLException but I can use ResultSet rs, Statement stmt, ResultSetMetaData rsmd inside my conversion function "to_roman_number()".

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • 1
    So if you know you can catch that exception what are you asking for then? Don't you want to catch it in that method but let it be thrown out of the method? – Thomas Sep 07 '22 at 11:28

0 Answers0