File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Help...JSP, MySql, and JDBC

Suzana Shah
Greenhorn

Joined: Jan 07, 2002
Posts: 12
Hi,
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;
%>
<html><body>
<%
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "", "");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM Table_one");
while (rs.next()) {
out.println(rs.getString("FName")+"<br>");
out.println(rs.getString("LName")+"<br>");
out.println(rs.getString("Age")+"<br>");
}
rs.close();
%>
</body></html>
I m saving this Temp.jsp file in the root directory of tomcat:
C:\jakarta-tomcat-3.3\webapps\ROOT
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 confused.....do i need to compile anything....I m pretty lost..??
Can anyone help....how do i get this to run?
Thanks,
Suzana
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
Hi!
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:
http://localhost:8080/yourfilename.jsp
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
Greenhorn

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?
http://localhost:8080/yourfilename.jsp
or do i have to mention :
http://localhost:8080/web-inf/class/filename.jsp
Can anyone help...i m
Suzana
David O'Meara
Rancher

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.
Dave.
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
ok.
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 ("org.gjt.mm.mysql.Driver").newInstance ();
sqlConn=java.sql.DriverManager.getConnection ("jdbc:mysql://localhost:3306/login?user=caven;password=winner");
sqlStmt=sqlConn.createStatement();
}
catch (Exception E) {
System.err.println("Unable to load driver.");
E.printStackTrace();
}
}
public void closed(){
try {
sqlRst.close();
sqlStmt.close();
sqlConn.close();
}
catch (Exception E) {
System.err.println("Unable to close driver.");
E.printStackTrace();
}
}
}
then you could use this class anywhere.


@_@ Life is so nice!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help...JSP, MySql, and JDBC
 
Similar Threads
retreiving all rows from a mysql table to a jsp page
how to upload and retrive a image from mysql database
Data source name not found and no default driver specified
JDBC/Servlets question
Confused...about JDBC,