Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Newbie question on JSP/Tomcat/MySql

 
Scott Matthews
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been learning how to use JSP pages for about 3 weeks. I am using Tomcat. I have a Html page that accepts a person's name. Then a JSP is called that puts the person's name into a java bean and displays it back to the screen. I have an html page, a jsp page, and java code for the bean. This I have working.
What I want now is that after accepting a person's name, I want the JSP page to additionally look up that person's name in a MySql database which holds the person's name and address. Then I will display either the person's address or a "not found" message.
Could someone provide a JSP page for a MySql database called MyNames that has FullName and Address and the field names to do the above example?
I have been attempting to read books on this and cannot understand the information.
Thanks for your help.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it the JDBC part you don't understand? I know that JDBC URL's can be quite daunting at first. It took me several times through to get a connection to databases... and it always takes me several tries for each new kind of database I've tried.

Sun has a JDBC tutorial online which helped me when I started out: http://java.sun.com/docs/books/tutorial/jdbc/index.html

This walks you through the database connection and using the statement and resultset objects.


Another thing you could do is post what you have so far, and we can show you what to fix...
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott
If its the database part your having trouble iwth post what you've got or what you're confused on...
I use mySQL here at work a lot and can probably give you a push in the right direction.

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Scott Matthews
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More specifics:
Honestly, I have not attempted to write any specific JSP code to connect and access the MySql database. I know this is not a good answer, but I am frustrated as to what to do using JSP pages.
Off the subject a little bit, but I have written a small stand alone java application to read a mySql database and display a few records.
The more I read on how to use a JSP page to access a database, the less clear it is. I understand the general concepts the book authors are describing, but they use their own custom tag libraries.
Should I write my own tag library to get information from a mySql database?
When I wrote the original email question, my hope was that someone would have a small example that would illustrate how to access a mySql database from a JSP page without using a tag library. Once I saw how this worked, I would then read more about developing my own tag library and attempt an example using my new custom library.

I was hoping that an example of someone having been down this road before could have written something like:
sample.jsp
------------------------
connect to the database using the MySql driver .... by the following statement:
...............
here is an example of an SQL statement to look up the field FullName field:
.........
the results of that query are stored in the following variable/vector, etc.
----------------
Summary:
I assure you I am not trying to be lazy and scurt the necessary learning pitfalls that strengthen the long term knowledge base.
I continue to read and reread to try and learn. I was hopeful someone could provide a sample JSP page with code for my small example using the field names I described previously. Or they could provide an example of their own JSP page with their field names and I could follow this as a pattern. But, maybe accessing databases needs to be from tag libraries, I don't know.
I appreciate your time. Any JSP code samples or additional direction as to what I can read would be greatly appreciated.
[This message has been edited by Scott Matthews (edited October 17, 2001).]
 
Pranit Saha
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi scott,
First of all u've to understand the JDBC things.. then u should've the code like this..
Class.forName("Your driver");
Connection.conn = DriverManager.getConnection("your DSN");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select address from tablename where empid='"+empid+"'");
if(rs!=null)
{
if(rs.next())
{
//display address
}
else
{
//this employee doesn't exists
}
}

Originally posted by Scott Matthews:
More specifics:
Honestly, I have not attempted to write any specific JSP code to connect and access the MySql database. I know this is not a good answer, but I am frustrated as to what to do using JSP pages.
Off the subject a little bit, but I have written a small stand alone java application to read a mySql database and display a few records.
The more I read on how to use a JSP page to access a database, the less clear it is. I understand the general concepts the book authors are describing, but they use their own custom tag libraries.
Should I write my own tag library to get information from a mySql database?
When I wrote the original email question, my hope was that someone would have a small example that would illustrate how to access a mySql database from a JSP page without using a tag library. Once I saw how this worked, I would then read more about developing my own tag library and attempt an example using my new custom library.

I was hoping that an example of someone having been down this road before could have written something like:
sample.jsp

 
Scott Matthews
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I understand this part of it. The code you provided is very similar to the working code I have in my stand alone application that accesses a mySql data I have.
What I want now is how to do this type of thing from a JSP page.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18162
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes, I understand this part of it. The code you provided is very similar to the working code I have in my stand alone application that accesses a mySql data I have.
What I want now is how to do this type of thing from a JSP page.

There's no magic to it. It if works standalone, it should work in a JSP. The only difference is that for high-volume sites, it's better to get the connection from a connection pool to reduce the per-page system overhead, but that's not something to worry about if you're just getting started.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, I'm echoing Tim.

You should be able to wholesale cut and paste your JDBC code into your JSP page... just put the whole thing in between <% %> tags and it will work.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic