File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes MySQL data to show on Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "MySQL data to show on Tomcat" Watch "MySQL data to show on Tomcat" New topic
Author

MySQL data to show on Tomcat

SW Tang
Greenhorn

Joined: Jul 11, 2004
Posts: 2
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

Joined: Feb 18, 2004
Posts: 68
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

Joined: Jul 11, 2004
Posts: 2
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

Joined: Feb 18, 2004
Posts: 68
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

Joined: Jan 10, 2002
Posts: 60046
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MySQL data to show on Tomcat
 
Similar Threads
exception's
Need Help Pulling Data From Oracle Table (Newby)
Can I create connection pooling with data sources using sybase driver(jconn2.jar)?
Currency Problem + General Advice
junk values written into a file when column values taken from the database table..