Problem in Displaying the Mysql data in Browser using Jsp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xtremebass
    New Member
    • Nov 2008
    • 38

    Problem in Displaying the Mysql data in Browser using Jsp

    Hi ,
    i have created table with only one field for the type of varchar(8000) . I am stroed bulk amount of data in that field.

    The size i have specified is enough for the field to occupy my data.

    I am storing "Text file" as Data to that filed.when i am viewing contents present in the table it shows what actual contents present in that text file like one by one line of data.

    But when i am reterving the data in the browser using Jsp, it shows all contents present in the filed is displayed without any alignment. i want see output as i have seen/stored in the database.

    could please suggest me the right way to proceed this is problem .. is this the problem in mysql or Jsp?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Get the text into a string then use the replaceAll method to replace all \n with <BR />.
    Careful with your regex for matching \n. You need four \ to match one \.

    Comment

    • xtremebass
      New Member
      • Nov 2008
      • 38

      #3
      thanks for reply..
      actually the problem is ,

      database -table -record is to be updated through linux, from where some text file has to be created and shell script is executed for storing the text file to database as record in single field.after storing the text file it has to be retrieved in browser using Jsp program.

      so how i can replace the \n to <br \> tag when i dont have a \n in my text file. i really confused.

      database table data shown

      | total: used: free: shared: buffers: cached:
      Mem: 515952640 510210048 5742592 0 2199552 118583296
      Swap: 1077469184 572231680 505237504
      MemTotal: 503860 kB
      MemFree: 5608 kB
      MemShared: 0 kB
      Buffers: 2148 kB
      Cached: 62224 kB
      SwapCached: 53580 kB
      Active: 382952 kB
      ActiveAnon: 354608 kB
      ActiveCache: 28344 kB
      Inact_dirty: 72288 kB
      Inact_laundry: 14928 kB
      Inact_clean: 6720 kB
      Inact_target: 95376 kB
      HighTotal: 0 kB
      HighFree: 0 kB
      LowTotal: 503860 kB
      LowFree: 5608 kB
      SwapTotal: 1052216 kB
      SwapFree: 493396 kB
      HugePages_Total : 0
      HugePages_Free: 0
      Hugepagesize: 4096 kB |

      1 row in set (0.44 sec)


      code attached
      Code:
      <%@ page import="java.sql.*" %>
      <%@ page import="javax.servlet.*" %>
      <%@ page import="javax.sql.*" %>
      <%@ page import="javax.servlet.http.*" %>
      
      <%
      ResultSet rs=null;
      Connection con =null;
       %>
      
      <html>
      <head>
      <h3>Sample Database Reterival</h3></head>
      <table><tr><th>Memory Information</th></tr>
      
      <body>
      
      <%
      try
      {
      Class.forName("com.mysql.jdbc.Driver");
      con=DriverManager.getConnection("jdbc:mysql://192.168.1.8:3306/test","root","prastsys");
      Statement st=con.createStatement();
      rs=st.executeQuery("select * from charchk");
      while(rs.next())
      {
      String mem=rs.getString(1);
      %>
      
      <tr>
      <td>
      <%
      out.println(mem);
      %>
      <br>
      </td>
      <br>
      </tr>
      <%
      }
      %>
      </table>
      <%
      }
      catch(Exception e)
      {
      out.println(e);
      }
      %>
      
      </body>
      </html>
      output from browser:

      Sample Database Reterival

      Memory Information
      total: used: free: shared: buffers: cached:
      Mem: 515952640 510210048 5742592 0 2199552 118583296 Swap: 1077469184 572231680 505237504
      MemTotal: 503860 kB MemFree: 5608 kB MemShared: 0 kB Buffers: 2148 kB Cached: 62224 kB SwapCached: 53580 kB
      Active: 382952 kB ActiveAnon: 354608 kB ActiveCache:
      28344 kB Inact_dirty: 72288 kB Inact_laundry: 14928
      kB Inact_clean: 6720 kB Inact_target: 95376 kB HighTotal: 0 kB HighFree: 0 kB LowTotal: 503860 kB LowFree: 5608 kB SwapTotal:
      1052216 kB SwapFree: 493396 kB HugePages_Total : 0 HugePages_Free: 0 Hugepagesize: 4096 kB

      --

      this is what the output displayed in browser. there is no order /alignment has done when displayed but mysql shows the contents by one by one line in order .. so please tel me some detailed solution for this..

      Thank you

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        1.) Better remove all that database connection code from the JSP. The JSP must only get the results and display them.
        2.) Did you try passing that string to a replaceAll method like I suggested?

        Comment

        • xtremebass
          New Member
          • Nov 2008
          • 38

          #5
          Originally posted by r035198x
          1.) Better remove all that database connection code from the JSP. The JSP must only get the results and display them.
          2.) Did you try passing that string to a replaceAll method like I suggested?

          i dont know how pass the string to replace all method that you would suggested?
          i dont have '\n' in my text file or a string i have,then how can i use replace all method to convert \n to <br />tag?
          help me.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            [CODE=java]String mem=rs.getStrin g(1);
            mem = mem.replaceAll( "\\\\n","<B R />");[/CODE]

            Comment

            • xtremebass
              New Member
              • Nov 2008
              • 38

              #7
              Originally posted by r035198x
              [CODE=java]String mem=rs.getStrin g(1);
              mem = mem.replaceAll( "\\\\n","<B R />");[/CODE]
              still i am not get the required output after applying the replaceAll method that you would suggested . is there any other solution for this problem?

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Can you post the code you used?

                Comment

                • xtremebass
                  New Member
                  • Nov 2008
                  • 38

                  #9
                  Originally posted by r035198x
                  Can you post the code you used?
                  Here is the code i used.
                  Code:
                  <%@ page import="java.sql.*" %>
                  <%@ page import="javax.servlet.*" %>
                  <%@ page import="javax.sql.*" %>
                  <%@ page import="javax.servlet.http.*" %>
                  
                  <%
                  ResultSet rs=null;
                  Connection con =null;
                   %>
                  
                  <html>
                  <head>
                  <h3>Sample Database Reterival</h3></head>
                  <!--<table><tr><th>Memory Information</th></tr>-->
                  
                  <body>
                  
                  <%
                  try
                  {
                  Class.forName("com.mysql.jdbc.Driver");
                  con=DriverManager.getConnection("jdbc:mysql://192.168.1.8:3306/test","root","prastsys");
                  Statement st=con.createStatement();
                  rs=st.executeQuery("select * from charchk");
                  while(rs.next())
                  {
                   String mem=rs.getString(1);
                   mem = mem.replaceAll("\\\\n","<BR />");
                  
                  out.println(mem);
                  %>
                  <br>
                  
                  <%
                  }
                  
                  }
                  catch(Exception e)
                  {
                  out.println(e);
                  }
                  %>
                  </body>
                  </html>

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    What if you wrap the string in <pre> </pre> tags?

                    P.S You really should not be connecting to databases in JSP code.

                    Comment

                    • xtremebass
                      New Member
                      • Nov 2008
                      • 38

                      #11
                      Originally posted by r035198x
                      What if you wrap the string in <pre> </pre> tags?

                      P.S You really should not be connecting to databases in JSP code.


                      I really i dont know what to proceed next? i think you are saying that dont use Jsp code for DB connection isn't it? then tell me how i can overcome this problem..can u give me an idea for this problem or any other alternative or how to proceed further??
                      Last edited by r035198x; Nov 14 '08, 09:55 AM. Reason: removed the unneccessary code tags!

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Basically mixing Java code and HTML is a bad idea and should be kept to a bare minimum. Doing so creates lots of maintenance headaches and makes your application slower. All the Java code should be in Servlets and utility classes. The JSP must only be concerned with getting results from those classes and displaying that data.

                        P.S Did you try the pre tags on the text?

                        P.PS How did you decide that the data is well formatted in the database? Did you run a select from the command line or similar?

                        Comment

                        • xtremebass
                          New Member
                          • Nov 2008
                          • 38

                          #13
                          i didnt use/try the [pre] tag in text.

                          you asked that, how did i decided Mysql table is well formatted?
                          for that i checked Mysql table using select query.

                          results showw like this,

                          mysql> select * from charchk;

                          | memory |
                          ------------------------------------------------------------------------
                          | total: used: free: shared: buffers: cached:
                          Mem: 515952640 510210048 5742592 0 2199552 118583296
                          Swap: 1077469184 572231680 505237504
                          MemTotal: 503860 kB
                          MemFree: 5608 kB
                          MemShared: 0 kB
                          Buffers: 2148 kB
                          Cached: 62224 kB
                          SwapCached: 53580 kB
                          Active: 382952 kB
                          ActiveAnon: 354608 kB
                          ActiveCache: 28344 kB
                          Inact_dirty: 72288 kB
                          Inact_laundry: 14928 kB
                          Inact_clean: 6720 kB
                          Inact_target: 95376 kB
                          HighTotal: 0 kB
                          HighFree: 0 kB
                          LowTotal: 503860 kB
                          LowFree: 5608 kB
                          SwapTotal: 1052216 kB
                          SwapFree: 493396 kB
                          HugePages_Total : 0
                          HugePages_Free: 0
                          Hugepagesize: 4096 kB |

                          ---------------------------------------------
                          so that i decided it is well formatted .. or tell me my thought is wrong with reason. actually i am not storing the value to that filed directly. fFom linux i have inserted record .

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            So you are trying to display many rows line by line rather than one formatted row?
                            Your break tag is not correct in your JSP. Make that <BR /> instead.

                            Comment

                            Working...