This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MySQL data to show on Tomcat

 
SW Tang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I am a greenhorn and very new on all these. Basically want to know how to have data on the MySQL database to show up in the Tomcat page. Desperately need some help.

Have the following installed.
MySQL 4.0 (working okay)
jdk 1.4.2
JSP 2.0 (working okay)
Tomcat 5.0 (working okay as samples of JSP shows up).

Sample coding:


import java.sql.*;
import java.io.*;

class StocksMy {
public static void main (String args []) {

try {

Class.forName("com.mysql.jdbc.Driver").newInstance();

try {

} catch (Exception e) {
System.out.println("StocksMy");
System.exit(1);
}

Connection conn =DriverManager.getConnection
("jdbc:mysql://:3306/StocksMy);

Statement stmt = conn.createStatement();

ResultSet rset = stmt.executeQuery ("select * from stocks");

while (rset.next()) {
String ticker=rset.getString(1).trim();
String title=rset.getString(2).trim();
String price=rset.getString(3).trim();
String blanks=
" ";

System.out.print (ticker);
System.out.print(blanks.substring(1,8-ticker.length()));
System.out.print (title);
System.out.print(blanks.substring(1,41-title.length()));
System.out.println (price);
}
} catch (Exception e) {
System.out.println("StocksMy: JDBC exception");
System.exit(1);
}
}
}


1) Is the above correct.
2) Do you need to do this in JSP first? How? Very much appreciated if you can show in the above sample.

Thank you all.
SW Tang
:roll: :roll: :roll:
[ July 11, 2004: Message edited by: Bear Bibeault ]
 
M. Gagnon
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class you've defined with method public static void main is not going to output to a web page. You probably want to make it a bean and use it in a JSP or make it a servlet and get the PrintWriter object to output. For example:

PrintWriter out = response.getWriter();
out.println(getYourText());
 
SW Tang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. Since I am really greenhorn, guess I have to go read more about bean or servlet somewhere to figure out.

Meanwhile would appreciate if you could complete the whole sequence of the sample in the bean or servlet for me to try out.

Thank you very very much.
 
M. Gagnon
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I could, but you'd learn a lot more by going through some tutorials or books and trying it on your own. Then you could come back to javaranch with more specific problems as you run into them. Sun has a tutorial you can download from:

http://java.sun.com/j2ee/1.4/download.html#tutorial

I'm sure you can use google to find more on the web. There are also some good books suggested here in the Bunkhouse. I like Marty Hall's "Core Java Servlets and Java Server Pages" 2nd edition. "Web Development with Java Server Pages" by Duane Fields and Mark Kolb is also great, but the latest edition doesn't cover JSP 2.0. Many people are awaiting the book by Kathy Sierra and Bert Bates that's due out this month ("Head First Servlets & JSP").

Good luck.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meanwhile would appreciate if you could complete the whole sequence of the sample in the bean or servlet for me to try out.


You are mistaking JavaRanch for somewhere you can go to get others to do your work for you. As M. Gagnon has pointed out, you will learn more by writing your code yourself and coming here for help on the parts that have you confused.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic