By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,180 Members | 1,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,180 IT Pros & Developers. It's quick & easy.

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

P: 10
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.

Expand|Select|Wrap|Line Numbers
  1.  ---------------------------------------------------------------------------------------------- 
  2. Class.forName ("com.mysql.jdbc.Driver").newInstance();
  3. out.println("<BR> Connecting to DB...Pls. Wait <BR>");
  4.  
  5. Connection con = DriverManager.getConnection("url","user","pwd");
  6.  
  7. if(con.isClosed())
  8. out.println("<BR><BR><BR>" +"Could NOT connect to MySQL Database...");
  9. else out.println("<BR> CONNECTED !!! <BR>");
  10.  
  11. Statement stmt = con.createStatement();
  12. results = stmt.executeQuery("SELECT * FROM TableName" );
  13.  
  14. -----------------------------------------------------------------
  15.  
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.
Expand|Select|Wrap|Line Numbers
  1. --------------------------------------------------------------------------
  2. Driver d = (Driver)Class.forName("com.mysql.jdbc.Driver").newInstance();
  3. out.println("<BR>Got a driver instance. ");
  4. if (d.acceptsURL(url)) out.println("<BR>The driver does accept my URL");
  5. else out.println("<BR>The driver doesn't like the URL I'm trying"); 
  6. ---------------------------------------------------------------------------
  7.  
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.
May 16 '07 #1
Share this Question
Share on Google+
13 Replies


10K+
P: 13,264
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.
May 16 '07 #2

P: 10
There is no error displayed.

Its as if that part of the code is ignored.

Thank you for the reply!
May 16 '07 #3

Expert 10K+
P: 11,448
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
May 16 '07 #4

P: 10
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.
'
May 16 '07 #5

Expert 10K+
P: 11,448
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
May 16 '07 #6

10K+
P: 13,264
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?
May 16 '07 #7

P: 10
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("able to ping");
}
catch(IOEXception e){}

Thank you again!!!

D
May 16 '07 #8

P: 10
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.
================================================== =========
May 16 '07 #9

P: 10
Do you get a blank page then? Or does it say page not found?

This is what the code looks like


Expand|Select|Wrap|Line Numbers
  1.  try{ 
  2. .....
  3. Class.forName ("com.mysql.jdbc.Driver").newInstance();
  4. out.println("<BR> Connecting to DB...Pls. Wait <BR>");
  5.  
  6. Connection con = DriverManager.getConnection("jdbc:mysql://blah.blah.net:3307/dbname","user","pwd");
  7.  
  8. if(con.isClosed())
  9. out.println("<BR><BR><BR>" +"Could NOT connect to MySQL server ");
  10. else out.println("<BR> CONNECTED !!! <BR>");
  11.  
  12. Statement stmt = con.createStatement();
  13. results = stmt.executeQuery("SELECT * FROM <tablename> WHERE code = '" + code + "'" );
  14.  
  15.             while(results.next())
  16.             {
  17.  
  18.                 //display
  19.             }
  20.  
  21.  
  22. catch(Exception e) {System.err.println("Exception: " + e.getMessage());} 
  23. finally {
  24. out.println("This part gets displayed");
  25. }
  26.  

The part in the finally block gets displayed.


Thanks,
D
May 16 '07 #10

10K+
P: 13,264
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.
May 16 '07 #11

P: 10
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

Expand|Select|Wrap|Line Numbers
  1.  try { 
  2. InetAddress address = InetAddress.getByName("url");
  3. out.println("<BR>Name: " + address.getHostName());
  4. out.println("<BR>Addr: " + address.getHostAddress());
  5. out.println("<BR>Reach: " + address.isReachable(3000));
  6. if(address.isReachable(3000)) 
  7. out.println("<BR><BR>able to reach url");
  8. else out.println("<BR><BR>Unable to reach url");
  9. }
  10. catch (UnknownHostException e) {
  11. out.println("<BR><BR> Unknown exception");
  12. }
  13. catch (IOException e) {
  14. out.println("<BR><BR>IO Exception");
  15. }
  16.  
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.AccessControlException: access denied (java.net.SocketPermission 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
May 16 '07 #12

P: 10
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.
May 16 '07 #13

10K+
P: 13,264
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.
May 17 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.