JDBC/JSP/MySQL Code works from my local machine, but not on server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • delram
    New Member
    • May 2007
    • 10

    JDBC/JSP/MySQL Code works from my local machine, but not on server

    Hello All!

    I've been struggling with this problem for a while.

    I have a database from which I need to read and display some data on a browser. (The database is set up for remote access).

    I'm using the following JSP/JDBC code to do that.

    [CODE=java] ----------------------------------------------------------------------------------------------
    Class.forName ("com.mysql.jdb c.Driver").newI nstance();
    out.println("<B R> Connecting to DB...Pls. Wait <BR>");

    Connection con = DriverManager.g etConnection("u rl","user","pwd ");

    if(con.isClosed ())
    out.println("<B R><BR><BR>" +"Could NOT connect to MySQL Database...");
    else out.println("<B R> CONNECTED !!! <BR>");

    Statement stmt = con.createState ment();
    results = stmt.executeQue ry("SELECT * FROM TableName" );

    -----------------------------------------------------------------
    [/CODE]

    When I run this of my local machine, it works fine. But when I upload it to a server, it doesn't run through. I dont get either the connected or not connected message.

    I tried this piece of code that I found online to check the driver.
    [CODE=java]
    --------------------------------------------------------------------------
    Driver d = (Driver)Class.f orName("com.mys ql.jdbc.Driver" ).newInstance() ;
    out.println("<B R>Got a driver instance. ");
    if (d.acceptsURL(u rl)) out.println("<B R>The driver does accept my URL");
    else out.println("<B R>The driver doesn't like the URL I'm trying");
    ---------------------------------------------------------------------------
    [/CODE]
    I ran it off the server and it worked. I got the outputs --Got a driver instance and The driver does accept my URL


    I'm unable to figure out why this code can be run locally from my machine, but not from a different location. The database is NOT on my machine.

    Any inouts will be really appreciated.

    I'm using an Apache Tomcat container and the database is MySQL.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by delram
    When I run this of my local machine, it works fine. But when I upload it to a server, it doesn't run through. I dont get either the connected or not connected message.

    What's the error that's displayed on the page.

    Comment

    • delram
      New Member
      • May 2007
      • 10

      #3
      There is no error displayed.

      Its as if that part of the code is ignored.

      Thank you for the reply!

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Your URL is incorrect; you tested it twice with 1) a database on your local
        machine and 2) on a server, where the URL denotes that local machine again.
        You need to pass the hostname or IP address of the machine that runs your
        database services in the URL too.

        kind regards,

        Jos

        Comment

        • delram
          New Member
          • May 2007
          • 10

          #5
          Hi Jos,

          Thank you for your time.

          The database is on another (3rd) machine.

          So I have

          1. My local machine
          2. Database hosted on a 2nd machine
          3. Place I'm moving my code to (its basically a hosting account provided by an external company)

          I can access it from my local machine. However, when I move the code over to another machine, it doesn't work.

          Shouldn't the url be the same irrespective of where I'm accessing the database from? Or did I misunderstand your reply?

          Thanks,
          D.
          '

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by delram
            Hi Jos,

            Thank you for your time.

            The database is on another (3rd) machine.

            So I have

            1. My local machine
            2. Database hosted on a 2nd machine
            3. Place I'm moving my code to (its basically a hosting account provided by an external company)

            I can access it from my local machine. However, when I move the code over to another machine, it doesn't work.

            Shouldn't the url be the same irrespective of where I'm accessing the database from? Or did I misunderstand your reply?

            Thanks,
            D.
            '
            Ah, ok, I must've misunderstood your previous post. Now it's just a networking
            matter. Can your 'other' machine 'see' the (database) machine? (hint: ping)

            Indeed, if the target database machine is the same it shouldn't matter on which
            client you're running your code if the network(s) are set up correctly.

            kind regards,

            Jos

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by delram
              There is no error displayed.

              Its as if that part of the code is ignored.

              Thank you for the reply!
              Do you get a blank page then? Or does it say page not found?

              Comment

              • delram
                New Member
                • May 2007
                • 10

                #8
                Thank you! It feels like I'm making some progress.

                Since the 3rd machine (that I called server) is essentially a web hosting account, I cannot try the ping directly.

                I will try and ping it with some Java code

                Something like

                try{
                ping ("url")
                out.println("ab le to ping");
                }
                catch(IOEXcepti on e){}

                Thank you again!!!

                D

                Comment

                • delram
                  New Member
                  • May 2007
                  • 10

                  #9
                  Originally posted by r035198x
                  Do you get a blank page then? Or does it say page not found?

                  Hi r035198x

                  That code is in the middle of a JSP page.

                  After that, I have a couple of out.println statements and a Continue button that redirects to another page. All the rest of the code works fine.

                  The part dealing with the database (where I'm trying to read and display some info) is "ignored".
                  =============== =============== =============== ==============
                  A little more background. Its for an online test-taking system where the students punch in a 'Test Code'.

                  I try to confirm their test code, display the questions and then direct them to a page with a form where they can type in their answers. This page essentially reads off a table and displays the questions that correspond to their test code.
                  =============== =============== =============== ==============

                  Comment

                  • delram
                    New Member
                    • May 2007
                    • 10

                    #10
                    Originally posted by r035198x
                    Do you get a blank page then? Or does it say page not found?

                    This is what the code looks like


                    [CODE=java] try{
                    .....
                    Class.forName ("com.mysql.jdb c.Driver").newI nstance();
                    out.println("<B R> Connecting to DB...Pls. Wait <BR>");

                    Connection con = DriverManager.g etConnection("j dbc:mysql://blah.blah.net:3 307/dbname","user", "pwd");

                    if(con.isClosed ())
                    out.println("<B R><BR><BR>" +"Could NOT connect to MySQL server ");
                    else out.println("<B R> CONNECTED !!! <BR>");

                    Statement stmt = con.createState ment();
                    results = stmt.executeQue ry("SELECT * FROM <tablename> WHERE code = '" + code + "'" );

                    while(results.n ext())
                    {

                    //display
                    }


                    }
                    catch(Exception e) {System.err.pri ntln("Exception : " + e.getMessage()) ;}
                    finally {
                    out.println("Th is part gets displayed");
                    }
                    [/CODE]


                    The part in the finally block gets displayed.


                    Thanks,
                    D

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by delram
                      Hi r035198x

                      That code is in the middle of a JSP page.

                      After that, I have a couple of out.println statements and a Continue button that redirects to another page. All the rest of the code works fine.

                      The part dealing with the database (where I'm trying to read and display some info) is "ignored".
                      =============== =============== =============== ==============
                      A little more background. Its for an online test-taking system where the students punch in a 'Test Code'.

                      I try to confirm their test code, display the questions and then direct them to a page with a form where they can type in their answers. This page essentially reads off a table and displays the questions that correspond to their test code.
                      =============== =============== =============== ==============
                      You can wrap that code in a try/catch block and out.println in the catches to print the exception stacktrace. It'd at least give you the exception if one was thrown.

                      Comment

                      • delram
                        New Member
                        • May 2007
                        • 10

                        #12
                        Hi Jos,

                        My local machine has WindowsXP and the remote runs Linux.

                        I can ping the database host machine from my local machine 1 (command prompt).
                        The database (2nd machine) is set up to be accessed remotely and has no security restrictions.

                        I tried this piece of code

                        [CODE=java] try {
                        InetAddress address = InetAddress.get ByName("url");
                        out.println("<B R>Name: " + address.getHost Name());
                        out.println("<B R>Addr: " + address.getHost Address());
                        out.println("<B R>Reach: " + address.isReach able(3000));
                        if(address.isRe achable(3000))
                        out.println("<B R><BR>able to reach url");
                        else out.println("<B R><BR>Unable to reach url");
                        }
                        catch (UnknownHostExc eption e) {
                        out.println("<B R><BR> Unknown exception");
                        }
                        catch (IOException e) {
                        out.println("<B R><BR>IO Exception");
                        }
                        [/CODE]
                        When I run this code from local(machine 1), it says unable to reach url, but does spit out the name, ip address of the host.The reachable is false on the local


                        On the 3rd machine(hosting account), I get this compilation error

                        java.security.A ccessControlExc eption: access denied (java.net.Socke tPermission url resolve)

                        I called the customer support for my hosting provider (machine 3) and they assure me that there are no restrictions in place.



                        Any thoughts ?

                        Pls reply when you can.

                        Thanks,
                        D

                        Comment

                        • delram
                          New Member
                          • May 2007
                          • 10

                          #13
                          Hello!

                          Thank you Jos and r035198x for your time and assistance in this matter.

                          I called up the hosting service provider again. The customer service rep. indicated that there might be restrictions on trying to access an external database. I apologize for taking up your time. The last time I called, they were pretty confident that there were NO restrictions of any kind at their end.

                          Once again, thanks for your time and assistance.

                          But I'm glad I discovered this forum !

                          D.

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by delram
                            Hello!

                            Thank you Jos and r035198x for your time and assistance in this matter.

                            I called up the hosting service provider again. The customer service rep. indicated that there might be restrictions on trying to access an external database. I apologize for taking up your time. The last time I called, they were pretty confident that there were NO restrictions of any kind at their end.

                            Once again, thanks for your time and assistance.

                            But I'm glad I discovered this forum !

                            D.
                            Ah, so Jos was right again.
                            Hope you can stay around and help others where you can as well.

                            Comment

                            Working...