?
Solved

Data from MYsql to JSP page

Posted on 2003-03-26
7
Medium Priority
?
212 Views
Last Modified: 2010-04-01
I know how to INSERT data from a JSP page to the database but how do i get the data from the database and put it into some field on my jsp page? An example would help!
0
Comment
Question by:TSK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 19

Accepted Solution

by:
cheekycj earned 80 total points
ID: 8212423
<%
  Connection dbconn;
  ResultSet results;
  PreparedStatement sql;

    try {
      Class.forName("org.gjt.mm.mysql.Driver").newInstance();

      try {
        dbconn = DriverManager.getConnection(
                     "jdbc:mysql://localhost/nutrition", "saundersk",
                     "kevin");
        sql = dbconn.prepareStatement(
                  "SELECT * FROM member");
        results = sql.executeQuery();
        out.println("<form>");
        while (results.next()) {
          out.println("<input type=text name=username value='" + results.getString("username") + "'>");
        }
        out.println("</form>");
      } catch (SQLException s) {
        out.println("SQL Error " + s.getMessage());
      }
     catch (Exception e) {
      out.println("General error:" + e.getMessage());
    }
    finally {
      if (results != null) results.close();
      if (sql != null) sql.close();
      if (dbconn != null) dbconn.close();
    }
%>

CJ
0
 

Author Comment

by:TSK
ID: 8212547
How do i make the data coming back from the database a JSP hyperlink to other page?

I want to make the user able to click my Data, which came from the database, and go to another page!
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8212651
as url parameters?

give me an example url

CJ
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:TSK
ID: 8212800
OK well i have this and i i get back three things from the database. I want to be able to click on the database data to go to the next page....say the next page is www.nextpage.come

<DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<%@ page
    import = "java.io.*"
    import = "java.lang.*"
    import = "java.sql.*"
%>
<TITLE>Diary Central </TITLE>
<META NAME="Generator" CONTENT="TextPad 4.4">
<META NAME="Author" CONTENT="?">
<META NAME="Keywords" CONTENT="?">
<META NAME="Description" CONTENT="?">
</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#008080" VLINK="#008080"
ALINK="#008080" BACKGROUND="?">
<table width="100%" height="75" border="0" cellpadding="25" align="center"
cellspacing="0" background="?">
<tr>
<td><img src="apple9.jpg" width="100" height="100" align="right">
<td><img src="logo.jpg" width="550" height="100"></td></td>
</tr>
</table>

<table width="90%" height="75" border="0" cellpadding="" align="center"
cellspacing="0" background="?">
<td height="44" colspan="2" bgcolor="#008080"><font face="Times New Roman"
color=#FFFFFF size="5"><b>&nbsp; &nbsp; &nbsp;  custom food.jsp </b></font></td>
</table><BR>

<form action ="edit custom food.jsp" method="get">

<%
  Connection dbconn;
  ResultSet results;
  PreparedStatement sql;
  try
  {
       Class.forName("org.gjt.mm.mysql.Driver").newInstance();
       try
       {
            String name;
            String user, pass1, pass2, email;
            boolean     doneheading = false;
           
            dbconn = DriverManager.getConnection("jdbc:mysql://localhost/nutrition","saundersk","kevin");
     
            sql = dbconn.prepareStatement("SELECT food_name FROM food");
     
            results = sql.executeQuery();
   
            while(results.next())
            {
                 if(! doneheading)
                 {
                      out.println("<table border=2>");
                      doneheading = true;
                     
                 }
             
                 name = results.getString("food_name");
         
                 out.println("<tr><td>" + name);
                 

            }
            if(doneheading)
            {
                 out.println("</table>");
            }
            else
            {
                 out.println("No matches for ");
            }
       }          
       catch (SQLException s)
       {
            out.println("SQL Error");
       }
  }
  catch (ClassNotFoundException err)
  {
       out.println("Class loading error");
     }
%>


<BR>
<table width="90%" height="15" border="0" cellpadding="" align="left"
cellspacing="0" background="?">
<td height="15" colspan="2" bgcolor="#008080"></td>
</table><BR>
<P ALIGN="left">©2003 Diary Central, All Rights Reserved.
</BODY>
</HTML>
0
 

Author Comment

by:TSK
ID: 8212814
do i need a date mask or format date to enter dates into my sql?
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8219100
I don't see the href in the above code.

as far as date goes.. you should require the user to enter it in certain acceptable formats.  If you can parse it.. then create a Date object and use a PreparedStatment to insert it into the DB.

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8231506
Thanx for the "A"

CJ
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question