• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help...JSP, MySql, and JDBC

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic