2 Replies Latest reply: Oct 20, 2007 10:13 AM by 807603 RSS

    Reading a string containing special character from a result set

    807603
      My Code....

      PrintStream p = new PrintStream(fout);

      if (connection == null)
      {
      connection = getConnection();
      }
      CallableStatement proc = connection.prepareCall("{call testing_read()}");

      rs = proc.executeQuery();

      while ( rs.next() )
      {

      String page2 = rs.getString(1);
      System.out.println(page2);
      }

      rs.getString(1); is a string value eg. 'Rebekah \n Govender' that is being set in a stored procedure.

      The output of my program is :'Rebekah \n Govender' , i need for it to read the special characters, and produce this output :

      Rebekah
      Govender

      Without using a result set , it works fine.

      eg.

      String page2 = 'Rebekah \n Govender'
      System.out.println(page2);

      Will give an output of :
      Rebekah
      Govender

      I need to read values from the results set, pls help me someone....




      Please help....
        • 1. Re: Reading a string containing special character from a result set
          807603
          Hi,

          you can try something like this:
          import java.io.UnsupportedEncodingException;;
          
          /**
           *  Main class, manages the complete game
           */
          public class Main {
            public static void main(String[] args) {
              String page2 = "Rebekah \n Govender";
              try {
                page2 = new String(page2.getBytes("ISO-8859-1"), "ETF-8");
              } catch (UnsupportedEncodingException e) {}
              System.out.println(page2);
               }
          }
          Regards,
          Hercog Mario
          • 2. Re: Reading a string containing special character from a result set
            807603
            String page2 = rs.getString(1).replace("\\n", "\n");
            When you include source code in your posts, please enclose it in CODE tags. There's a button for that above the input textarea.
            ThE-MaRaC wrote:
            Hi,

            you can try something like this:
            import java.io.UnsupportedEncodingException;;
            
            /**
            *  Main class, manages the complete game
            */
            public class Main {
            public static void main(String[] args) {
            String page2 = "Rebekah \n Govender";
            try {
            page2 = new String(page2.getBytes("ISO-8859-1"), "ETF-8");
            } catch (UnsupportedEncodingException e) {}
            System.out.println(page2);
                 }
            }
            Regards,
            Hercog Mario
            That is a disgusting hack which you shouldn't be recommending to anyone, and it's totally irrelevant here anyway. Also, as written, it does absolutely nothing because you misspelled "UTF-8" and you swallowed the exception that would have told you so.