Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

Data from MYsql to JSP page

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
TSK
Asked:
TSK
  • 4
  • 3
1 Solution
 
cheekycjCommented:
<%
  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
 
TSKAuthor Commented:
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
 
cheekycjCommented:
as url parameters?

give me an example url

CJ
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TSKAuthor Commented:
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
 
TSKAuthor Commented:
do i need a date mask or format date to enter dates into my sql?
0
 
cheekycjCommented:
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
 
cheekycjCommented:
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.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now