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

R

ram2375

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.

----------------------------------------------------------------------------------------------
Class.forName ("com.mysql.jdbc.Driver").newInstance();
out.println("<BR> Connecting to DB...Pls. Wait <BR>");

Connection con = DriverManager.getConnection("url","user","pwd");

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

Statement stmt = con.createStatement();
results = stmt.executeQuery("SELECT * FROM TableName" );


-----------------------------------------------------------------


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.
----------------------------------------------------------------------------------------------------------------
/*Driver d =
(Driver)Class.forName("com.mysql.jdbc.Driver").newInstance();
out.println("<BR>Got a driver instance. ");
if (d.acceptsURL(url)) out.println("<BR>The driver does accept my
URL");
else out.println("<BR>The driver doesn't like the URL I'm trying"); */
------------------------------------------------------------------------------------------------------------------

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.
 
N

Nino

I'm using the following JSP/JDBC code to do that.
--------------------------------------------------------------------------- -------------------
Class.forName ("com.mysql.jdbc.Driver").newInstance();
out.println("<BR> Connecting to DB...Pls. Wait <BR>");

Connection con = DriverManager.getConnection("url","user","pwd");

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

Statement stmt = con.createStatement();
results = stmt.executeQuery("SELECT * FROM TableName" );

-----------------------------------------------------------------

Try this instead:

try {
Class.forName(driver);
} catch (java.lang.ClassNotFoundException e) {
out.println("ClassNotFoundException " + e.getMessage());
} catch (Exception e) {
out.println("driver not "+e.getMessage()); }

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.

Just because the database is not on your server doesn't mean that the
driver shouldn't be either. Make sure the driver is actually where you
say it is on the server.

You might also be having a problems with the connection to the
database server. Is the URL correct? Can you actually connect from
your app server to the database server? Your driver might be trying to
connect, but just can't.

Does this return any errors?

try {
con = DriverManager.getConnection(url,""+user+"",""+pass+"");
stmt = con.createStatement();

String db_query = "select * from TABLE";
ResultSet db_result = stmt.executeQuery(db_query);

stmt.close();
con.close();

} catch (SQLException ex) {
out.println("SQLException " + ex.getMessage());
}

Nino
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top