Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
|
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 13th, 2005, 11:45 PM
Authorized User
 
Join Date: Jan 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need basic JSP -> Database tutorial

I used to program in ASP, and am trying to make the transition to JSP.

I've found basic tutorials on basic syntax, but haven't found anything which just gives the basics of connecting to a database.

I'm looking for something which will have basic database connection strings, how to make an ODBC connection (or connect to an Oracle database on Linux) and so forth.

Does anyone have any good ideas on where to point me?

Thanks,

--tgr

Thaddeus G. Reeves
Internet Dissemination
Church of Scientology Int'l
__________________
Thaddeus G. Reeves
Internet Dissemination
Church of Scientology Int\'l
 
Old January 14th, 2005, 02:14 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There are JDBC tutorials on the SUN Java site.

If you want to connect using JSP you may also need to refer to the documentation of the server you are using.

Most beginners use Apache Tomcat (myself included). Its free to download, very reliable (in my limited experience) and pretty easy to set up. Within the tomcat documetation there are tutorials on setting up JDBC, but these show how to do it with JNDI in the Tomcat Config files.

The advantage of setting it up this way is that connection pooling can be used. If you choose to use JDBC from your code, connection pooling is not supported.

Hope some of this makes sense!



 
Old January 14th, 2005, 02:17 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, heres a link for a tutorial:

http://java.sun.com/developer/online...ro/JDBC20.html

 
Old January 15th, 2005, 12:04 AM
Authorized User
 
Join Date: Jan 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

AngryCat -

Thanks much for the data. I checked out the tutorial you sent, and that looks relatively straightforward in terms of Java. What I'm used to in ASP is where one has code built into a page (or an Include) which connects to the database, gives username and password, and establishes the various data sets. Is the code on that tutorial useful to do something like this?

I'm first just trying to figure out how to do run-of-mill tasks like pulling a bunch of records from a SQL or Oracle database and displaying it on the screen, or taking data from a form and saving the record.

Do you know of any good tutorials for something like this? ASP101.com had tons of this stuff on ASP - I'm just looking for something similar in JSP.

Thanks a million for the help,

--tgr

Thaddeus G. Reeves
Internet Dissemination
Church of Scientology Int'l
 
Old January 16th, 2005, 12:36 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again,

As I garbled in my previous mail, there are different ways to connect to the database depending on the web server you are using.

As for the tutorial I gave you the link for, your right, its Java, however the code is easily adapted for JSP.

When you use ASP you are embedding VB between tags, the same with JSP using Java code, thus you can use java code directly in pages, once you understand the types of tag used.

There are a number of forums including JSP at:

http://forum.java.sun.com/index.jspa

I use Apache Tomcat, there are JDBC tutorials included with the download, but these are specific to Tomcat.

There is a JSP/JDBC tutorial (amongst other things) at

http://jakarta.apache.org/tomcat/tom...les-howto.html

Points 8 and 9 of the user guide on left of page.
These tutorials are for Tomcat, using JNDI, but some code is generic.

I think this Tomcat tutorial explains how to install database drivers which you will need to do before you can connect.



 
Old January 16th, 2005, 02:12 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Forgot to mention there's also a wrox database forum

http://p2p.wrox.com/forum.asp?FORUM_ID=110

There is code in here for accessing databses directly from JSP pages.
I havent any code like this myself as I use a different method.

Within JSP you can use Java Beans. These are pure Java classes which you can write and compile. I have my JDBC code in these classes (in fact most of my programming is done here). I then access these beans (classes) from my JSP pages. For databse access I call the method I need passing sql as a parameter and the bean returns the record/result set I require.

This keeps the JSP cleaner and allows re-use of code, but is not really what your looking for, yet :)

 
Old January 16th, 2005, 03:01 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually there is one bit of code I have which accesses a database directly from JSP, its a bit of a mess, but it does work. If you compare this to code in the JDBC Java tutorial, you will see what I mean about embedding Java code in JSP.

Import statements in Java are replaced here with lines like:

<%@ page import="java.sql.*" %>

You need these to access the API's you need.

As I said, this code is a mess, but at the bottom you will see:

rs.close();
st.close();
con.close();

These statements close the database conenction when finished, you will need these.

This code retreives and displays an image from a database in a jsp page.

<%@ page contentType="image/jpeg; chaoResult=iso-8859-1" language="java" %>
<%@ page session="false" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="java.io.*" %>

<html>
<head>
<title>Here is the title</title>
<meta http-equiv="refresh" content="10">
<body>
<table width="287" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td width="141">Time of last refresh:</td>
    <td width="140"> ..

    </td>
  </tr>
</table>



<%
    String databaseDriver = "org.gjt.mm.mysql.Driver";

    // Open a database connection

    Class.forName( databaseDriver );
    String databaseName = "jdbc:mysql://localhost/test";

    Connection con = null;
    try{
        con = DriverManager.getConnection(databaseName, "user","password");

        //response.setContentType("image/jpeg");

        String query = "select image from images where id =1"; // 1 is a literal value I used for testing

        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(query);
        rs.next();



        byte[] bytearray = new byte[4096];
int size=0;
InputStream sImage;
sImage = rs.getBinaryStream(1);
response.reset();
response.setContentType("image/jpeg");
response.addHeader("Content-Disposition","filename=getimage.jpeg");
while((size=sImage.read(bytearray))!= -1 )
    {
        response.getOutputStream().write(bytearray,0,size) ;
    }

        response.flushBuffer();
sImage.close();






%>
<DL>
 <table width="400" border="1" cellspacing="0" cellpadding="0">



<%
    String rowColor = "#33CCFF"; // blue

    //while (rs.next()) {
    Blob fName = rs.getBlob(1);




        //System.out.println("just above OutputStream");




        String sName = "";
        String phone = "";
        String address = "";
        if (rowColor.equals("#33CCFF"))
        {
            rowColor = "#66FFCC"; // green
        }
        else
        {
            rowColor = "#33CCFF";
        }
%>

   <tr bgcolor= <%= rowColor %>>
    <td width="150"><%= "<img src=\"" + ".jpg\">"%></td>
    <td width="150"><%= sName %></td>
    <td width="150"><%= phone %></td>
    <td width="150"><%= address %></td>
  </tr>







<%
    //}
    rs.close();
    rs = null;

    st.close();
    st = null;
    }

    finally {
        if (con != null )
        {
            con.close();
        }
    }

%>
</table>
</DL>
</BODY>
</HTML>


 
Old January 17th, 2005, 03:13 AM
Authorized User
 
Join Date: Jan 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

AngryCat -

Thanks much for your help on that -- this was exactly what I was looking for. Your assistance & looking around for me is much appreciated.

I'll let you know if it all works!

--tgr

Thaddeus G. Reeves
Internet Dissemination
Church of Scientology Int'l





Similar Threads
Thread Thread Starter Forum Replies Last Post
<jsp:useBean> action problem austinf JSP Basics 1 August 21st, 2006 04:23 PM
JSP Tutorial websites?? cyrusds_asp JSP Basics 2 September 2nd, 2005 02:08 AM
looking for tutorial on how to buld database app method Access 8 April 18th, 2005 09:40 AM
<Visual Basic.NET and AQL Server 2000:B&EDL>?Code? cqliucheng Wrox Book Feedback 1 September 2nd, 2004 08:13 AM
<jsp:setProperty /> pedersen JSP Basics 3 September 30th, 2003 08:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.