import java.net.URL;
import java.sql.*;

class repro2 {

   public static void main (String args[]) {
      String url = "jdbc:mariadb://lnxx64r7:3307/db999_lnxpleu14" ;
      String drptab = "DROP TABLE TESTNUM";
      String crttab = "CREATE TABLE TESTNUM (N9X2 NUMERIC(9,2), N20X4 NUMERIC (20,4))";
      String seltab = "SELECT N9X2, N20X4  FROM TESTNUM";
      Connection con = null;
      DatabaseMetaData dma = null; 

      System.out.println("\n\n"); 
      System.out.println("BUG SYNOPSIS: Wrong precision is returned ");
      System.out.println("for NUMERIC columns. The returned precision is 2 greater than it should be");
      System.out.println("");                              

      try {

         // Load the MariaDB jdbc driver

         Class.forName ("org.mariadb.jdbc.Driver");

//       DriverManager.setLogStream(System.out);
   
         // Attempt to connect to a driver.  Each one
         // of the registered drivers will be loaded until
         // one is found that can process this URL

         DriverManager.setLoginTimeout(15);

         con = DriverManager.getConnection (
            url, "R729999D", "edaport");

         // If we were unable to connect, an exception
         // would have been thrown.  So, if we get here,
         // we are successfully connected to the URL

         // Check for, and display and warnings generated
         // by the connect.

         checkForWarning (con.getWarnings ());

         // Get the DatabaseMetaData object and display
         // some information about the connection

         dma = con.getMetaData ();

         System.out.println("\nConnected to " + dma.getURL());
         System.out.println("Driver       " + 
         dma.getDriverName());
         System.out.println("Version      " +
         dma.getDriverVersion());
         System.out.println("");
      }

      catch (SQLException ex) {

         // A SQLException was generated.  Catch it and
         // display the error information.  Note that there
         // could be multiple error objects chained
         // together

         System.out.println ("\n*** SQLException caught ***\n");

         while (ex != null) {
            System.out.println ("SQLState: " +
               ex.getSQLState ());
            System.out.println ("Message:  " + ex.getMessage ());
            System.out.println ("Vendor:   " +
               ex.getErrorCode ());
            ex = ex.getNextException ();
            System.out.println ("");
         }
         return;
      }

      catch (java.lang.Exception ex) {

         // Got some other type of exception.  Dump it.
         ex.printStackTrace ();
         return;
      }


      Statement stmt = null;

      try {

         System.out.println("\nexecute "+crttab);
         stmt = con.createStatement();
         stmt.execute(drptab);
      }

      catch (SQLException ex) 
      { 
         while (ex != null) 
         {
            System.out.println ("Message:  " + ex.getMessage ());
            ex = ex.getNextException ();
         }
      }

      try {
         stmt.execute(crttab);

         System.out.println("\nexecute "+seltab);
         ResultSet rs = stmt.executeQuery(seltab);

         System.out.println("\nget resultset metadata");
         ResultSetMetaData rsmd = rs.getMetaData();

         System.out.println("\n N9X2 precision is "+ rsmd.getPrecision(1));
         System.out.println(" N9X2 scale is "+ rsmd.getScale(1));
         System.out.println("\n N20X4 precision is "+ rsmd.getPrecision(2));
         System.out.println(" N20X4 scale is "+ rsmd.getScale(2));
         
         // Close the connection
         rs.close();
         con.close();
      }

      catch (SQLException ex) {
         // A SQLException was generated.  Catch it and
         // display the error information.  Note that there
         // could be multiple error objects chained
         // together

         System.out.println ("\n*** SQLException caught ***\n");

         while (ex != null) {
            System.out.println ("SQLState: " +
               ex.getSQLState ());
            System.out.println ("Message:  " + ex.getMessage ());
            System.out.println ("Vendor:   " +
               ex.getErrorCode ());
            ex = ex.getNextException ();
            System.out.println ("");

         }
      }

      catch (java.lang.Exception ex) {
   
         // Got some other type of exception.  Dump it.

         ex.printStackTrace ();
      }
   } // end of main


   //-------------------------------------------------------------------
   // checkForWarning
   // Checks for and displays warnings.  Returns true if a warning
   // existed
   //-------------------------------------------------------------------

   private static boolean checkForWarning (SQLWarning warn) 
      throws SQLException  {
      boolean rc = false;

      // If a SQLWarning object was given, display the
      // warning messages.  Note that there could be
      // multiple warnings chained together

      if (warn != null) {
         System.out.println ("\n *** Warning ***\n");
         rc = true;
         while (warn != null) {
            System.out.println ("SQLState: " +
               warn.getSQLState ());
            System.out.println ("Message:  " +
               warn.getMessage ());
            System.out.println ("Vendor:   " +
               warn.getErrorCode ());
            System.out.println ("");
               warn = warn.getNextWarning ();
         }
      }
      return rc;
   }
}
