You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Anup Niroula <an...@gmail.com> on 2011/02/21 14:55:05 UTC

connecting jsp with mysql

Hi,

I am having difficulty in connecting jsp and mysql using Tomcat 7.0 .
I downloaded jdbc driver "mysql-connector-java-5.1.15-bin.jar" and placed it
in two locations:

   - CATALINE_HOME\lib
   - C:\Program Files\Apache Software Foundation\Tomcat
   7.0\webapps\lsc\WEB-INF\lib

I tried to connect using the following code but it always says:"Cannot
connect to database server".
 Do i need to make any configurations apart from this ?
I tried to connect with the following code:
  <%@page language="java" import="java.sql.*"%>
        <%
  Connection conn = null;

          try
           {
               String userName = "root";
               String password = "nbuser";
               String url = "jdbc:mysql://localhost:3306/test";
               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
               conn = DriverManager.getConnection (url, userName, password);
               out.println ("Database connection established");
           }
           catch (Exception e)
           {
               out.println ("Cannot connect to database server");
           }
           finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.close ();
                       out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
           }
%>

RE: connecting jsp with mysql

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Anup Niroula [mailto:anup.niroula@gmail.com] 
> Subject: connecting jsp with mysql

> I downloaded jdbc driver "mysql-connector-java-5.1.15-bin.jar" 
> and placed it in two locations:
>    - CATALINE_HOME\lib
>    - C:\Program Files\Apache Software Foundation\Tomcat
>    7.0\webapps\lsc\WEB-INF\lib

Fatal error.  Place the jar file in *one* location only.  If you're using Tomcat's DBCP capability, it must go into CATALINA_HOME/lib; if your webapp is doing all the DB connection handling, it must be in the webapp's WEB-INF/lib directory.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: connecting jsp with mysql

Posted by Mikolaj Rydzewski <mi...@ceti.pl>.
 On Mon, 21 Feb 2011 07:55:05 -0600, Anup Niroula 
 <an...@gmail.com> wrote:
>            catch (Exception e)
>            {
>                out.println ("Cannot connect to database server");
>            }

 I guess the answer is hidden within 'e'.

 Anyway, you should rather use JNDI 
 http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html
 Or at least any framework (spring, dbutils, etc).

-- 
 Mikolaj Rydzewski <mi...@ceti.pl>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: connecting jsp with mysql

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Anup,

On 2/21/2011 10:20 AM, Anup Niroula wrote:
> Hi,
> out.println ("Cannot connect to database server message "+e.getMessage());
> "This gives following error msg:
> "Cannot connect to database server com.mysql.jdbc.Driver."

How about e.printStackTrace()? Post the entire message and full stack trace.

I can see a whole bunch of things wrong with your original code:

1. You re-register the driver for every request (Class.forName) which
   isn't that big of a deal... just a waste of time at least for the
   MySQL driver. It's mostly just bad coding practice.

2. You create a new Connection for every request. You would be much
   better off using pooled connections. Look at the Tomcat documentation
   for using JNDI DataSources.

3. You swallow nearly all exceptions. At least log them.
   System.err/System.out doesn't count.

4. You shouldn't have any Java code in a JSP (debatable).

5. Everything is hard-coded. Of course, this looks like a test page
   so you can be excused for that :)

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk1kFrEACgkQ9CaO5/Lv0PC62QCeIpgG7Wu3fZNcNKfsm+5Mxn4w
3RwAniITH/47q9NrkdiqZK2CgPJWUhFA
=+hIO
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: connecting jsp with mysql

Posted by Anup Niroula <an...@gmail.com>.
Hi,
out.println ("Cannot connect to database server message "+e.getMessage());
"This gives following error msg:
"Cannot connect to database server com.mysql.jdbc.Driver."
I am using windows 7.

On Mon, Feb 21, 2011 at 8:44 AM, Martin Gainty <mg...@hotmail.com> wrote:

>
> 1)putting a lightly loaded "engine light on" text message is neither
> helpful nor delivers any useful information to those of us trying to
> determine your error
> always include the message from the exception object e.g.
> out.println ("Cannot connect to database server message="+e.getMessage());
>
> 2)
> netstat -a | grep 3306
> if you dont see tcp connection displayed on your output then mysql daemon
> is down..restart it
>
> 3)never put username and password on emails ..this is not a secure way to
> do business
> btw try implementing the same username to get into mysql
> $MYSQL_HOME/bin/mysql.exe --host=localhost --user=whatever_user
> --password=whatever_password --host=localhost
>
> Martin Gainty
> ______________________________________________
> Jogi és Bizalmassági kinyilatkoztatás/Verzicht und
> Vertraulichkeitanmerkung/Note de déni et de confidentialité
>  Ez az
> üzenet bizalmas.  Ha nem ön az akinek szánva volt, akkor kérjük, hogy
> jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának
> készítése nem megengedett.  Ez az üzenet csak ismeret cserét szolgál és
> semmiféle jogi alkalmazhatósága sincs.  Mivel az electronikus üzenetek
> könnyen megváltoztathatóak, ezért minket semmi felelöség nem terhelhet
> ezen üzenet tartalma miatt.
>
> Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene
> Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte
> Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht
> dient lediglich dem Austausch von Informationen und entfaltet keine
> rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von
> E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
> Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le
> destinataire prévu, nous te demandons avec bonté que pour satisfaire
> informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie
> de ceci est interdite. Ce message sert à l'information seulement et n'aura
> pas n'importe quel effet légalement obligatoire. Étant donné que les email
> peuvent facilement être sujets à la manipulation, nous ne pouvons accepter
> aucune responsabilité pour le contenu fourni.
>
>
>
>
> > Date: Mon, 21 Feb 2011 07:55:05 -0600
> > Subject: connecting jsp with mysql
> > From: anup.niroula@gmail.com
> > To: users@tomcat.apache.org
> >
> > Hi,
> >
> > I am having difficulty in connecting jsp and mysql using Tomcat 7.0 .
> > I downloaded jdbc driver "mysql-connector-java-5.1.15-bin.jar" and placed
> it
> > in two locations:
> >
> >    - CATALINE_HOME\lib
> >    - C:\Program Files\Apache Software Foundation\Tomcat
> >    7.0\webapps\lsc\WEB-INF\lib
> >
> > I tried to connect using the following code but it always says:"Cannot
> > connect to database server".
> >  Do i need to make any configurations apart from this ?
> > I tried to connect with the following code:
> >   <%@page language="java" import="java.sql.*"%>
> >         <%
> >   Connection conn = null;
> >
> >           try
> >            {
> >                String userName = "root";
> >                String password = "nbuser";
> >                String url = "jdbc:mysql://localhost:3306/test";
> >                Class.forName ("com.mysql.jdbc.Driver").newInstance ();
> >                conn = DriverManager.getConnection (url, userName,
> password);
> >                out.println ("Database connection established");
> >            }
> >            catch (Exception e)
> >            {
> >                out.println ("Cannot connect to database server");
> >            }
> >            finally
> >            {
> >                if (conn != null)
> >                {
> >                    try
> >                    {
> >                        conn.close ();
> >                        out.println ("Database connection terminated");
> >                    }
> >                    catch (Exception e) { /* ignore close errors */ }
> >                }
> >            }
> > %>
>
>

RE: connecting jsp with mysql

Posted by Martin Gainty <mg...@hotmail.com>.
1)putting a lightly loaded "engine light on" text message is neither helpful nor delivers any useful information to those of us trying to determine your error 
always include the message from the exception object e.g.
out.println ("Cannot connect to database server message="+e.getMessage());

2)
netstat -a | grep 3306
if you dont see tcp connection displayed on your output then mysql daemon is down..restart it

3)never put username and password on emails ..this is not a secure way to do business 
btw try implementing the same username to get into mysql 
$MYSQL_HOME/bin/mysql.exe --host=localhost --user=whatever_user --password=whatever_password --host=localhost

Martin Gainty 
______________________________________________ 
Jogi és Bizalmassági kinyilatkoztatás/Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 Ez az
üzenet bizalmas.  Ha nem ön az akinek szánva volt, akkor kérjük, hogy
jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának
készítése nem megengedett.  Ez az üzenet csak ismeret cserét szolgál és
semmiféle jogi alkalmazhatósága sincs.  Mivel az electronikus üzenetek
könnyen megváltoztathatóak, ezért minket semmi felelöség nem terhelhet
ezen üzenet tartalma miatt.

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.




> Date: Mon, 21 Feb 2011 07:55:05 -0600
> Subject: connecting jsp with mysql
> From: anup.niroula@gmail.com
> To: users@tomcat.apache.org
> 
> Hi,
> 
> I am having difficulty in connecting jsp and mysql using Tomcat 7.0 .
> I downloaded jdbc driver "mysql-connector-java-5.1.15-bin.jar" and placed it
> in two locations:
> 
>    - CATALINE_HOME\lib
>    - C:\Program Files\Apache Software Foundation\Tomcat
>    7.0\webapps\lsc\WEB-INF\lib
> 
> I tried to connect using the following code but it always says:"Cannot
> connect to database server".
>  Do i need to make any configurations apart from this ?
> I tried to connect with the following code:
>   <%@page language="java" import="java.sql.*"%>
>         <%
>   Connection conn = null;
> 
>           try
>            {
>                String userName = "root";
>                String password = "nbuser";
>                String url = "jdbc:mysql://localhost:3306/test";
>                Class.forName ("com.mysql.jdbc.Driver").newInstance ();
>                conn = DriverManager.getConnection (url, userName, password);
>                out.println ("Database connection established");
>            }
>            catch (Exception e)
>            {
>                out.println ("Cannot connect to database server");
>            }
>            finally
>            {
>                if (conn != null)
>                {
>                    try
>                    {
>                        conn.close ();
>                        out.println ("Database connection terminated");
>                    }
>                    catch (Exception e) { /* ignore close errors */ }
>                }
>            }
> %>