LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-25-2004, 05:01 AM   #1
blakeless
LQ Newbie
 
Registered: Sep 2003
Posts: 28

Rep: Reputation: 15
Tomcat and mysql - HELP!


Firstly, Im struggling with connecting jsp to mysql on Debian Woody - i have installed j2sdk1.3, tomcat4 and mysql and i have also installed libmysql-java - Java database (JDBC) driver for MySQL to connect mysql to tomcat - I can not get a script to connect to the mysql - its say "can not connect to database"- Can anyone point me in the right direction?

Second problem, I'm looking for a script where you can add text, etc and it saves it to a html page in a directory?

Cheers Blake
 
Old 09-27-2004, 10:44 PM   #2
jandersen78
LQ Newbie
 
Registered: Sep 2004
Location: Provo, UT
Distribution: Fedora Core 2
Posts: 15

Rep: Reputation: 0
Is mysql running on the same box as the jsp? Can you login to mysql using the mysql client? "Can not connect to database" sounds like either a network problem or the mysql database isn't up. Once you get past that you'll be into the world of permissions problems. Would it be possible to post the JSP/Servlet you're using to connect to MySQL?
 
Old 09-28-2004, 03:28 AM   #3
blakeless
LQ Newbie
 
Registered: Sep 2003
Posts: 28

Original Poster
Rep: Reputation: 15
reply about jdbc to mysql

Thanks for replying - yes everything is on the same box

Here is the code:

import java.sql.*;
public class Connect
{
public static void main (String[] args)
{
Connection conn = null;
try
{

String userName = "testuser";
String password = "testpass";
String url = "jdbc:mysql://localhost/test";
Class.forName ("org.gjt.mm.mysql.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
}
finally
{
try
{
if (conn != null)
{
conn.close ();
System.out.println ("Database connection terminated");
}
}
catch (Exception e) { /* ignore close errors */ }
}
}
}

In mysql, i created a user testuser and testpass and granted all privilages for that user on database test.
You wouldnt happen to have any better code to try would you?
Also do I need to put any jar files anywhere and if so, where - I have no idea which ones and how to put them in the CLASSPATH

Cheers Blake
 
Old 09-28-2004, 09:19 AM   #4
jandersen78
LQ Newbie
 
Registered: Sep 2004
Location: Provo, UT
Distribution: Fedora Core 2
Posts: 15

Rep: Reputation: 0
Yes, you definitely need the jar containing the mysql JDBC connector in you WEB_INF/lib directory (that's the easiest place to put it...). Jar files in the WEB-INF/lib directory should automatically be added to the classpath by the servlet/JSP container (tomcat). Here's some code I have used to create MySQL connections in java:

public static Connection getConnection() {
Connection conn= null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/your_db_name", "user", "pass"));
} catch (ClassNotFoundException cnfe) {
System.err.println("Class not found: " + cnfe.getMessage());
} catch (Exception e) {
System.err.println("general exception: " + e.getMessage());
}
return conn;
}

you might want to try including the connection port (3306) explicitly. I thought 3306 was assumed by default but that might not be true.

So you are able to login to Mysql just by using the mysql client right? That would ensure that the database is up and running...
 
Old 09-28-2004, 06:02 PM   #5
blakeless
LQ Newbie
 
Registered: Sep 2003
Posts: 28

Original Poster
Rep: Reputation: 15
thankyou again - you are really helping me.

Well I can log in to mysql:
mysql -h localhost -u testuser -ptestpass, and there is a default 'test' database, thats about all ive done for mysql - Anything else? Do I need to grant prviliages for that user to that database?


plus where should the mysql-connector-java-3.0.15-ga.jar file go ?- there are multiple WEB-INF directories, for example:
/var/lib/tomcat4/webapps/ROOT/WEB-INF
/var/lib/tomcat4/webapps/examples/WEB-INF/
/var/lib/tomcat4/webapps/webdav/WEB-INF

i think it might ignore CLASSPATH, so I really wanted to actually put the jar file in the required directory - I have not added anything to the class path.

Once again, thanks for your help, much appreciated..

Cheers Blake
 
Old 09-28-2004, 08:46 PM   #6
blakeless
LQ Newbie
 
Registered: Sep 2003
Posts: 28

Original Poster
Rep: Reputation: 15
one more thing

I tried your code, file named Connection.java: - I made a few alterations:

////////////////////////////////////////////////////

import java.sql.*;

public class Connection

{

public static Connection getConnection() {
Connection conn= null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "testuser", "testpass");
} catch (ClassNotFoundException cnfe) {
System.err.println("Class not found: " + cnfe.getMessage());
} catch (Exception e) {
System.err.println("general exception: " + e.getMessage());
}
return conn;
}

}

/////////////////////////////////////////////////

and this is the result:

Connection.java:11: incompatible types
found : java.sql.Connection
required: Connection
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "testuser", "testpass");
^
1 error


Cheers Blake
 
Old 09-28-2004, 10:03 PM   #7
jandersen78
LQ Newbie
 
Registered: Sep 2004
Location: Provo, UT
Distribution: Fedora Core 2
Posts: 15

Rep: Reputation: 0
first, about that incompatible types error, I think you should try changing the name of the class your using from Connection as that looks like it's creating a name conflict with java.sql.Connection.

second, the jar file needs to go in the WEB-INF directory of the webapp that you're working on. The simplest case is that you're creating a folder in the tomcat/webapps directory. This then becomes your webapp and has a minimum structure like this:

mywebapp
|--myfile.jsp
|--WEB-INF
|--web.xml
|--lib
|--all the jar files for the application
|--classes
|---all the java packages you're developing

You would then access this application at http://localhost:8080/mywebapp/myfile.jsp for a default tomcat install. This may not be an adequate explanation; if you need more google for information about webapps. You really ought to have a good understanding of the standard structure of a webapp
 
Old 09-28-2004, 10:04 PM   #8
jandersen78
LQ Newbie
 
Registered: Sep 2004
Location: Provo, UT
Distribution: Fedora Core 2
Posts: 15

Rep: Reputation: 0
whoops, looks like they stripped out my leading spaces...

mywebapp
|--myfile.jsp
|--WEB-INF
__|--web.xml
__|--lib
____|--all the jar files for the application
__|--classes
____|---all the java packages you're developing
 
Old 09-28-2004, 10:21 PM   #9
blakeless
LQ Newbie
 
Registered: Sep 2003
Posts: 28

Original Poster
Rep: Reputation: 15
Its actually a .java file, not .jsp

I changed the name of the class to Connection1.java - it now compiles fine however when I use Java:

vipatechnology:/var/lib/tomcat4/webapps/examples/WEB-INF/classes# java Connection1

I GET:

Exception in thread "main" java.lang.NoSuchMethodError: main
 
Old 09-28-2004, 10:43 PM   #10
blakeless
LQ Newbie
 
Registered: Sep 2003
Posts: 28

Original Poster
Rep: Reputation: 15
Also

Also in the directory:

:/var/lib/tomcat4/webapps/examples/WEB-INF

i have a directory called 'classes', 'jsp' and a file called web.xml - So should I create a webapp in the WEB-INF directory and then insert the ' mysql-connector-java-3.0.15-ga-bin.jar' in that newly created webapp directory?

Cheers Blake
 
Old 09-28-2004, 11:05 PM   #11
blakeless
LQ Newbie
 
Registered: Sep 2003
Posts: 28

Original Poster
Rep: Reputation: 15
sorry

ok, this seems to be the structure:

/var/lib/tomcat4/webapps/ROOT/ - this seems to be the default directory with an index.hhtml in it - when i go

http://localhost:8180 - i get that index page

Also in that ROOT directory is:
WEB-INF jakarta-banner.gif tomcat-power.gif tomcat.gif (including the index.html file)

in WEB-INF is web.xml - does this help at all?

Cheers Blake
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Slackware 10.2 + Mysql + Apache + PHP + Tomcat Black187 Slackware 5 11-16-2005 07:11 AM
Slackware 10.2 + Mysql + Apache + PHP + Tomcat Black187 Linux - General 1 11-16-2005 02:11 AM
Access MySQL, Webmin, Tomcat, Apache treotan Linux - Newbie 1 12-04-2004 04:22 PM
Getting MySQL to work with Tomcat Travis86 Linux - Software 3 07-14-2004 11:18 PM
Tomcat Works Withouth MySQL and dies with MySQL Guru3 Linux - Software 0 10-19-2003 07:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration