File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Help...JSP, MySql, and JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Help...JSP, MySql, and JDBC" Watch "Help...JSP, MySql, and JDBC" New topic

Help...JSP, MySql, and JDBC

Suzana Shah

Joined: Jan 07, 2002
Posts: 12
I have the following settings:
platform : Win98
Tomcat version 3.3
Mysql version mysql-3.23.38
JDBC Driver: mm.mysql-2.0.4-bin
JDK 1.3
Now i got tomcat running, and also the jsp examples running, mysql is also running fine on its own and i created a simple database with one table. Now i have put the JDBC driver in the C:\jakarta-tomcat-3.3\lib directory. As far as i know i dont need to specify any classpath as Tomcat manages finding and loading classes itself.
Now i created a simple jsp file(Temp.jsp) where i want to display the fields from the mysql database table:
here is my code:
<%@ page import="java.sql.*" %>
String connectionURL = "jdbc:mysql://localhost:3306/Temp?user=Suzana;password=pass";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(connectionURL, "", "");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM Table_one");
while ( {
I m saving this Temp.jsp file in the root directory of tomcat:
But now, once i created the jsp file and the database, how to actually run all this?
I m pretty new to this and i m i need to compile anything....I m pretty lost..??
Can anyone do i get this to run?
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
First of all , when you create the objects connection etc at the start its better to put them in directive statements rather than scriplets like this: (with the %! rather than %)

after you create your jsp page you dont ned to compile it.
just request it from your browser.
if you are using tomcat than type the following URL in your broweser:
make sure the file is available in the web-inf\class directory in tomcat.
you dont have to btw, but then specify the directory in the url.
hope it helped.
Suzana Shah

Joined: Jan 07, 2002
Posts: 12
Thanks Roy,
I just wanted to know that is there a difference between the two %! and % ?
And when you mentioned about putting the file under the web-inf\classes directory, i m totally confused about this...cause some say you should put it under the webapps\ROOT directory, some people say to put it under webapps\examples\jsp directory, this is so confusing..can you please tell me which one is the best directory?
If i do put the jsp file in the web-inf/class directory then how would i call this in the url?
is it this way as u mentioned?
or do i have to mention :
Can anyone help...i m
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Actually, the complete opposite is true.
It is better to put variables in scriptlets rather than directive blocks, since otherwise your JSP will not be (or is unlikely to be) thread safe.
This has moved away from JDBC, so I'm moving it to the JSP forum.
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
first of all i have to take back my suggestion to you before and agree with the bartender.
in this case it is better to put it as scriplet (the <% and %> ).
when you declare variables in the declaration like this : <%! %>
then the variables or functions you declrae will be availbe in the servlat as instance varaibles (if you declare in a scriplet then they will be put inside the service method of the jsp page, like doget and dopost).
as for where putting jsp files, servlets files etc here is the complete guide:
webapps\Root : all your html files, jsp files and imae files.
webapps\root\web-inf: the web.xml file .
webapps\root\web-inf\classes : all your java classes like beans servlets etc.
webapps\root\web-inf\lib: all your jar files like tag libraries.
thats it !

sorry about the mix up before. it was 4:00 am when i read that post.
Caven Wang
Ranch Hand

Joined: Jul 07, 2001
Posts: 31
why not use a javabean to connect to your database. it is much easier to operate.

public class dbCon {
public java.sql.Connection sqlConn;
public java.sql.Statement sqlStmt;
public java.sql.ResultSet sqlRst;
public dbCon() {
public void connected(){
try {
Class.forName ("").newInstance ();
sqlConn=java.sql.DriverManager.getConnection ("jdbc:mysql://localhost:3306/login?user=caven;password=winner");
catch (Exception E) {
System.err.println("Unable to load driver.");
public void closed(){
try {
catch (Exception E) {
System.err.println("Unable to close driver.");
then you could use this class anywhere.

@_@ Life is so nice!
I agree. Here's the link:
subject: Help...JSP, MySql, and JDBC
It's not a secret anymore!