Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

trouble connecting to mysql

 
   Database Help (Home) -> Java RSS
Next:  create a recordset  
Author Message
naveen

External


Since: Apr 06, 2008
Posts: 5



(Msg. 1) Posted: Sun Apr 06, 2008 10:03 am
Post subject: trouble connecting to mysql
Archived from groups: comp>lang>java>databases (more info?)

hello friends
i have just touched jsp programming and i m having trouble connecting
to mysql database.
I m using tomcat 6

the connection part of the code of my jsp (HelloMySql.jsp) is-->
______________________________________________________



..
..
..
<form name="sqlaccess" action="HelloMysql.jsp">
.......
</form>

<%
String dbURL="jdbc:mysql:localhost\\neo";
String username="root";
String passwd="abc";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(dbURL,username,passwd);
Statement stat=null;
try{
stat=conn.createStatement();
if(varSql.length() !=0 ){
%>
..........
_____________________________________________

i m using this driver -->
mysql-connector-java-5.1.6-bin.jar
and have put it in this location
C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib

further i have appended this to the Classpath environment variable -->
C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\mysql-
connector-java-5.1.6-bin.jar

but the sever returns error that
No suitable driver found

please tell me what else i am missing

thanks

 >> Stay informed about: trouble connecting to mysql 
Back to top
Login to vote
Arne_Vajhøj

External


Since: Mar 02, 2008
Posts: 37



(Msg. 2) Posted: Sun Apr 06, 2008 3:07 pm
Post subject: Re: trouble connecting to mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

naveen wrote:
> hello friends
> i have just touched jsp programming and i m having trouble connecting
> to mysql database.
> I m using tomcat 6
>
> the connection part of the code of my jsp (HelloMySql.jsp) is-->

> String dbURL="jdbc:mysql:localhost\\neo";
> String username="root";
> String passwd="abc";
> try{
> Class.forName("com.mysql.jdbc.Driver");
> Connection conn=DriverManager.getConnection(dbURL,username,passwd);

> i m using this driver -->
> mysql-connector-java-5.1.6-bin.jar
> and have put it in this location
> C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib
>
> further i have appended this to the Classpath environment variable -->
> C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\mysql-
> connector-java-5.1.6-bin.jar
>
> but the sever returns error that
> No suitable driver found

Unless you want to use container managed connection pool you
should put the jar file in your web apps WEB-INF/lib.

The connection URL looks wrong as well. It should be
something like:

"jdbc:mysql://localhost/neo"

Arne

 >> Stay informed about: trouble connecting to mysql 
Back to top
Login to vote
Lew

External


Since: Feb 15, 2008
Posts: 43



(Msg. 3) Posted: Sun Apr 06, 2008 3:54 pm
Post subject: Re: trouble connecting to mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Arne Vajhøj wrote:
> naveen wrote:
>> hello friends
>> i have just touched jsp programming and i m having trouble connecting
>> to mysql database.
>> I m using tomcat 6
>>
>> the connection part of the code of my jsp (HelloMySql.jsp) is-->
>
>> String dbURL="jdbc:mysql:localhost\\neo";
>> String username="root";
>> String passwd="abc";
>> try{
>> Class.forName("com.mysql.jdbc.Driver");
>> Connection conn=DriverManager.getConnection(dbURL,username,passwd);
>
>> i m using this driver -->
>> mysql-connector-java-5.1.6-bin.jar
>> and have put it in this location
>> C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib
>>
>> further i have appended this to the Classpath environment variable -->
>> C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\mysql-
>> connector-java-5.1.6-bin.jar
>>
>> but the sever returns error that
>> No suitable driver found
>
> Unless you want to use container managed connection pool you
> should put the jar file in your web apps WEB-INF/lib.
>
> The connection URL looks wrong as well. It should be
> something like:
>
> "jdbc:mysql://localhost/neo"

Also, it's pretty pointless to keep calling
Class.forName("com.mysql.jdbc.Driver");
after the first time in a program run. Once the driver is loaded, it's
loaded, and loading it again is redundant.

Also, move your Java source code ("scriptlet") out of the JSP and into a Java
servlet source file. Research the "Model-View-Controller" (MVC) pattern and
what Sun calls the "Model 2" web-app architecture. Among other things, it
will refactor your database connection code back two layers where it belongs.

--
Lew
 >> Stay informed about: trouble connecting to mysql 
Back to top
Login to vote
naveen

External


Since: Apr 06, 2008
Posts: 5



(Msg. 4) Posted: Mon Apr 07, 2008 2:28 am
Post subject: Re: trouble connecting to mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Apr 7, 12:07 am, Arne Vajhøj wrote:
> naveen wrote:
> > hello friends
> > i have just touched jsp programming and i m having trouble connecting
> > to mysql database.
> > I m using tomcat 6
>
> > the connection part of the code of my jsp (HelloMySql.jsp) is-->
> > String dbURL="jdbc:mysql:localhost\\neo";
> > String username="root";
> > String passwd="abc";
> > try{
> >    Class.forName("com.mysql.jdbc.Driver");
> >    Connection conn=DriverManager.getConnection(dbURL,username,passwd);
> > i m using this driver -->
> > mysql-connector-java-5.1.6-bin.jar
> > and have put it in this location
> > C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib
>
> > further i have appended this to the Classpath environment variable -->
> > C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\mysql-
> > connector-java-5.1.6-bin.jar
>
> > but the sever returns error that
> > No suitable driver found
>
> Unless you want to use container managed connection pool you
> should put the jar file in your web apps WEB-INF/lib.
>

well i have put it the jar file there too.

> The connection URL looks wrong as well. It should be
> something like:
>
> "jdbc:mysql://localhost/neo"

well i thought that you have to provide the location of the database
in this place "jdbc:mysql://localhost/neo"
though i have tried putting database as //localhostneo also
but this didnt work too

please suggest something
or if you could give me a working example it would be very kind of
you
thanks
 >> Stay informed about: trouble connecting to mysql 
Back to top
Login to vote
joe.weinstein

External


Since: Feb 21, 2005
Posts: 11



(Msg. 5) Posted: Mon Apr 07, 2008 9:23 am
Post subject: Re: trouble connecting to mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Apr 7, 2:28 am, naveen wrote:
> On Apr 7, 12:07 am, Arne Vajhøj wrote:
>
>
>
> > naveen wrote:
> > > hello friends
> > > i have just touched jsp programming and i m having trouble connecting
> > > to mysql database.
> > > I m using tomcat 6
>
> > > the connection part of the code of my jsp (HelloMySql.jsp) is-->
> > > String dbURL="jdbc:mysql:localhost\\neo";
> > > String username="root";
> > > String passwd="abc";
> > > try{
> > > Class.forName("com.mysql.jdbc.Driver");
> > > Connection conn=DriverManager.getConnection(dbURL,username,passwd);
> > > i m using this driver -->
> > > mysql-connector-java-5.1.6-bin.jar
> > > and have put it in this location
> > > C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib
>
> > > further i have appended this to the Classpath environment variable -->
> > > C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\mysql-
> > > connector-java-5.1.6-bin.jar
>
> > > but the sever returns error that
> > > No suitable driver found
>
> > Unless you want to use container managed connection pool you
> > should put the jar file in your web apps WEB-INF/lib.
>
> well i have put it the jar file there too.
>
> > The connection URL looks wrong as well. It should be
> > something like:
>
> > "jdbc:mysql://localhost/neo"
>
> well i thought that you have to provide the location of the database
> in this place "jdbc:mysql://localhost/neo"
> though i have tried putting database as //localhostneo also
> but this didnt work too
>
> please suggest something
> or if you could give me a working example it would be very kind of
> you
> thanks

It has to be "jdbc:mysql://localhost/neo" not //localhostneo.

Do this code:

Driver d = new com.mysql.jdbc.Driver();
System.out.println("Will the mysql driver accept the URL '" + dbURL +
"'? " + d.acceptsURL(dbURL) );

When you get it to return true, then you have a better chance of
connecting...
Joe
 >> Stay informed about: trouble connecting to mysql 
Back to top
Login to vote
Arne_Vajhøj

External


Since: Mar 02, 2008
Posts: 37



(Msg. 6) Posted: Mon Apr 07, 2008 9:40 pm
Post subject: Re: trouble connecting to mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

naveen wrote:
> On Apr 7, 12:07 am, Arne Vajhøj wrote:
>> Unless you want to use container managed connection pool you
>> should put the jar file in your web apps WEB-INF/lib.
>
> well i have put it the jar file there too.

Not too. Only there.

You should not put jar files all over the system.

>> The connection URL looks wrong as well. It should be
>> something like:
>>
>> "jdbc:mysql://localhost/neo"
>
> well i thought that you have to provide the location of the database
> in this place "jdbc:mysql://localhost/neo"

No. You connect to localhost (on port 3306) and select database
neo. The MySQL servers knows where it is actually stored.

> though i have tried putting database as //localhostneo also
> but this didnt work too

Unless you have a host called localhostneo then it will not work.

> please suggest something
> or if you could give me a working example it would be very kind of

<%
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/Test", "", "");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM T1");
....

works for me.

Arne
 >> Stay informed about: trouble connecting to mysql 
Back to top
Login to vote
naveen

External


Since: Apr 06, 2008
Posts: 5



(Msg. 7) Posted: Tue Apr 08, 2008 7:05 pm
Post subject: Re: trouble connecting to mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> > though i have tried putting database as //localhostneo also
> > but this didnt work too

well i am soory ..my mistake i meant //localhost/neo


> <%
> Class.forName("com.mysql.jdbc.Driver");
> Connection con =
> DriverManager.getConnection("jdbc:mysql://localhost/Test", "", "");
> Statement stmt = con.createStatement();
> ResultSet rs = stmt.executeQuery("SELECT * FROM T1");
> ...
>
> works for me.

well i have used:
________________________________________________________
....
<%
String varSql = request.getParameter("sql");
if (varSql == null) {
varSql = "";
}
varSql = java.net.URLDecoder.decode(varSql).trim();
%>
....
<%
String dbURL="jdbc:mysql://localhost/neo";
String username="root";
String passwd="abc";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(dbURL,username,passwd);
Statement stat=null;
try{
stat=conn.createStatement();
if(varSql.length() !=0 ){
%>
<pre> <%= varSql %> </pre>
<%
if (stat.execute(varSql)) {
java.sql.ResultSet rs = stat.getResultSet();
try {
java.sql.ResultSetMetaData metaData = rs.getMetaData();
int colCount =metaData.getColumnCount();
%>
....
________________________________________________________

now i must be making a mistake somewhere since the browser shows this:

_____________________________________________________
An Exception was caught while connecting to the database.

java.sql.SQLException: Communication link failure:
java.io.IOException, underlying cause: Unexpected end of input stream

** BEGIN NESTED EXCEPTION **

java.io.IOException
MESSAGE: Unexpected end of input stream

STACKTRACE:

java.io.IOException: Unexpected end of input stream
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:1096)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:626)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1562)
at com.mysql.jdbc.Connection.(Connection.java:491)
at
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:
346)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.apache.jsp.HelloMysql_jsp._jspService(HelloMysql_jsp.java:9Cool
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
393)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
290)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:
206)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:
230)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:
175)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:
128)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:
104)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:
109)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
261)
at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:
844)
at org.apache.coyote.http11.Http11Protocol
$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:
447)
at java.lang.Thread.run(Unknown Source)
____________________________________________________

what should i do about this?
please help
 >> Stay informed about: trouble connecting to mysql 
Back to top
Login to vote
Arne_Vajhøj

External


Since: Mar 02, 2008
Posts: 37



(Msg. 8) Posted: Tue Apr 08, 2008 10:24 pm
Post subject: Re: trouble connecting to mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

naveen wrote:
>>> though i have tried putting database as //localhostneo also
>>> but this didnt work too
>
> well i am soory ..my mistake i meant //localhost/neo

As a general rule: copy paste when you post to avoid typos.

> <%
> String varSql = request.getParameter("sql");
> if (varSql == null) {
> varSql = "";
> }
> varSql = java.net.URLDecoder.decode(varSql).trim();
> %>
> ...
> <%
> String dbURL="jdbc:mysql://localhost/neo";
> String username="root";
> String passwd="abc";
> try{
> Class.forName("com.mysql.jdbc.Driver");
> Connection conn=DriverManager.getConnection(dbURL,username,passwd);
> Statement stat=null;
> try{
> stat=conn.createStatement();

> java.sql.SQLException: Communication link failure:
> java.io.IOException, underlying cause: Unexpected end of input stream
>
> ** BEGIN NESTED EXCEPTION **
>
> java.io.IOException
> MESSAGE: Unexpected end of input stream
>
> STACKTRACE:
>
> java.io.IOException: Unexpected end of input stream
> at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:1096)
> at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:626)
> at com.mysql.jdbc.Connection.createNewIO(Connection.java:1562)
> at com.mysql.jdbc.Connection.(Connection.java:491)
> at
> com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:
> 346)
> at java.sql.DriverManager.getConnection(Unknown Source)
> at java.sql.DriverManager.getConnection(Unknown Source)
> at org.apache.jsp.HelloMysql_jsp._jspService(HelloMysql_jsp.java:9Cool
> at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

You have loaded the driver OK, you have established a TCP connection
to the server OK, but the JDBC driver and the MySQL server will not
talk to each other. It look like the MySQL server closes the connection.

Check that the JDBC driver you are using are newer than the MySQL
server version you are using.

Check in the MySQL side for errors.

Arne
 >> Stay informed about: trouble connecting to mysql 
Back to top
Login to vote
naveen

External


Since: Apr 06, 2008
Posts: 5



(Msg. 9) Posted: Wed Apr 09, 2008 6:02 am
Post subject: Re: trouble connecting to mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> You have loaded the driver OK, you have established a TCP connection
> to the server OK, but the JDBC driver and the MySQL server will not
> talk to each other. It look like the MySQL server closes the connection.
>
> Check that the JDBC driver you are using are newer than the MySQL
> server version you are using.
>

i am using |mysql-connector-java-5.1.6| connector
and |mysql-5.0.51a-win32| mysql database
i think these are latest versions and probably should comply with each
other.

> Check in the MySQL side for errors.
>
how do i do that ? please elucidate
other than this, i tried connecting to mysql
using jndi as described in the apache tomcat documentation

but it's example jsp uses jstl
the test.jsp is of form:
________________________________



<sql:query var="rs" dataSource="jdbc/TestDB">
select id, foo, bar from testdata
</sql:query>

<html>
<head>
<title>DB Test</title>
</head>
<body>

<h2>Results</h2>

<c:forEach var="row" items="${rs.rows}">
Foo ${row.foo}<br/>
Bar ${row.bar}<br/>
</c:forEach>

</body>
</html>
____________________________________________

the described jndi example works but i dont know jstl
and i need to use simple jsp coding

now if anyone would do me a favor by converting the test.jsp as shown
to a simple jsp (i.e. without using the jstl) then my problem would
stand
solved for now

somebody please help
 >> Stay informed about: trouble connecting to mysql 
Back to top
Login to vote
Lew

External


Since: Feb 15, 2008
Posts: 43



(Msg. 10) Posted: Wed Apr 09, 2008 7:52 pm
Post subject: Re: trouble connecting to mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Arne advised (and naveen neglected to attribute):
>> Check in the MySQL side for errors.

naveen wrote:
> how do i [sic] do that ? please elucidate

www.mysql.com

> the described jndi [sic] example works but i [sic] dont know jstl [sic]

<http://java.sun.com/javaee/5/docs/tutorial/doc/bnakc.html>

> now if anyone would do me a favor by converting the test.jsp as shown
> to a simple jsp (i.e. without using the jstl) then my problem would
> stand solved for now

That's funny!

--
Lew
 >> Stay informed about: trouble connecting to mysql 
Back to top
Login to vote
naveen

External


Since: Apr 06, 2008
Posts: 5



(Msg. 11) Posted: Thu Apr 10, 2008 11:56 am
Post subject: Re: trouble connecting to mysql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>
> > now if anyone would do me a favor by converting the test.jsp as shown
> > to a simple jsp (i.e. without using the jstl) then my problem would
> > stand solved for now
>
> That's funny!
>

man! u really have got a big funny bone Smile
however thanks guys at last i made the cut.
though through jndi

thanks ..again
 >> Stay informed about: trouble connecting to mysql 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
MySQL: going from 4.1 to 5.0.. - I have MySQL 4.1 installed in my machine (W2000), would like to upgrade to 5.0.. before I do that I need to know two things: 1) do I need to un-intall 4.1 first, and 2) what will happen to my DB's? if I un-install 4.1 will that also delete DB's? (I...

MySql installation. -

mysql path - Hello. If I create a new schema on mysql : Which folder is the schema created, and how external program "know" where the schema is ? Is there any general attitude, that I should know exactly where is the db file exists ? What is on connectio...

MySql with Hebrew. - Hello. I want to use MySql with Hebrew. I am using also Squirrel as a navigator. How can I use in mysql either Squirrel with Hebrew. Thanks :)

Sun buys out MySQL - Sun bought out opensource MySQL on 2008-02-28. http://mysql.com/news-and-events/sun/marten_mickos.html -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
   Database Help (Home) -> Java All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]