Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

connecting to MySQL db from Tomcat..

Thread view: 
Frances Del Rio - 20 Apr 2005 19:34 GMT
I have managed to connect fine to a db on my website server, thus:

   String url =
"jdbc:mysql://localhost:3306/francesdelrio_com?user=frances&password=XXXXXX";
  Statement stmt;
  ResultSet rs = null;
  Class.forName("com.mysql.jdbc.Driver").newInstance();)
  con = DriverManager.getConnection(url);

but somehow in Tomcat (locally on my machine, Win2k) I'm having a hard
time..

paths:
Tomcat app:  C:\tomcat\webapps\movies\index.jsp
db:  C:\Program Files\MySQL\MySQL Server 4.1\data\one
driver:  C:\Program
Files\mysql-connector-java-3.1.6\mysql-connector-java-3.1.6-bin.jar

but:  jdbc connection needs "localhost" in url..  but my driver is just
in c:\program files...

how do I connect?  is it possible at all to connect to a MySQL db from
Tomcat w/in a local machine?  thank you..  Frances
Daniel Rohe - 21 Apr 2005 10:54 GMT
Put the Connector/J JAR-file in the directory <tomcat-install>/common/lib
and then try to connect from a Servlet or JSP. This should work.
The default MySQL server opens the port 3306 on your local machine for
communication. This port is used by the Connector/J to connect to the
server, send queries and retrieving results.

Daniel

>I have managed to connect fine to a db on my website server, thus:
>
[quoted text clipped - 19 lines]
> how do I connect?  is it possible at all to connect to a MySQL db from
> Tomcat w/in a local machine?  thank you..  Frances
Frances Del Rio - 21 Apr 2005 15:03 GMT
man.. thank you..  this worked!!!  I want to know one thing, however,
that has me a bit confused..

this is code now:

String url =  "jdbc:mysql://localhost:3306/one?user=root&password=XXXX";
   Class.forName("com.mysql.jdbc.Driver").newInstance();

how does Tomcat know where to find db?  which is in
C:\Program Files\MySQL\MySQL Server 4.1\data

how on earth does Tomcat know how to find db?
(I mean "localhost:3306" still doesn't specify path to db.... right?)
it knows how to find driver, b/c I put it in Tomcat location where you
told me, but this says nothing to Tomcat about where db is.. (or even
tell the driver where to find db.. does this make sense?)

again many thanks......  :)  Frances

> Put the Connector/J JAR-file in the directory <tomcat-install>/common/lib
> and then try to connect from a Servlet or JSP. This should work.
[quoted text clipped - 27 lines]
>>how do I connect?  is it possible at all to connect to a MySQL db from
>>Tomcat w/in a local machine?  thank you..  Frances
Arnaud Berger - 21 Apr 2005 15:07 GMT
Hi,

When you connect to port 8080 of your tomcat, you also don't have to know
where tomcat is installeds but
you just want the page you required to display, right ?

The database just got an open socket on port 3306, waiting for client
connections.
once a client connects, begins a dialog through this socket, in which the
client sends his requests, and
in which the db just send its results.

It's as simple as that.

Regards,

Arnaud

> man.. thank you..  this worked!!!  I want to know one thing, however,
> that has me a bit confused..
[quoted text clipped - 47 lines]
> >>how do I connect?  is it possible at all to connect to a MySQL db from
> >>Tomcat w/in a local machine?  thank you..  Frances
Aleksandar Pecanov - 28 Apr 2005 16:36 GMT
Well... at first you have to put the driver jar file into a classpath
directory, copying it into c:\tomcat\shared\lib would do it for
starters.
Make sure the mysql server is running. Jdbc driver does not work in an
embedded mode. A more clear variant of your code would be:
   Class.forName("com.mysql.jdbc.Driver");
   Connection con = DriverManager.getConnection(url, user, password);

... where url, user and password are variables.
And one more thing: mysql would try to identify you using the username,
password and the host you are connecting from. I don't know how this
works under windows, but you might need to add user@host accounts in
mysql for hosts like your machine name or 127.0.0.1 to connect properly.

Good luck.

> I have managed to connect fine to a db on my website server, thus:
>
[quoted text clipped - 19 lines]
> how do I connect?  is it possible at all to connect to a MySQL db from
> Tomcat w/in a local machine?  thank you..  Frances


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.