We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

count command in sql using jsp

Raftor
Raftor asked
on
Medium Priority
1,250 Views
Last Modified: 2008-01-16
What is the format used for the count command using jsp.
This is what im using,

int cnt = stmt.execteQuery("SELECT count('"+id+"') FROM members ");

any suggestion of a good site that shows how to use mysql with jsp effectively.
Comment
Watch Question

ResultSet rs = stmt.execteQuery("SELECT count(id) FROM members ");

int count = rs.getInt(1);

Author

Commented:
That didnt work either if gave this error,

The server encountered an internal error () that prevented it from fulfilling this request.

Do you know of any sites that have sql and jsp tutorials?

Commented:
ResultSet rs = stmt.execteQuery("SELECT count \("+id+"\) FROM members ");
int count = rs.getInt(1);

if you want to count all rows in a table just do

ResultSet rs = stmt.execteQuery("SELECT count \(*\) FROM members");
int count = rs.getInt(1);

Commented:
jsp uses the exact same codes as java to do sql, as a matter of fact it is java.

so just search tutorial jdbc on www.google.com 
for sql tutorial go here: http://www.w3schools.com/sql/default.asp

Author

Commented:
no none of those worked either.
ooops my fault again...

ResultSet rs = stmt.execteQuery("SELECT count(id) FROM members ");
if(rs.next())
int count = rs.getInt(1);

Author

Commented:
 This is the code i am using. It would only work if i put it in braces. But the problem is now is that i cannot access count outside the braces.              

                Statement st = con.createStatement();
            ResultSet res = st.executeQuery("SELECT count(id) FROM members ");
            if(res.next()){
                  String count = res.getString(1);
            }
            out.println("the count: " + count);

This is the error i get when i try to access count outside the braces.

symbol  : variable count
location: class org.apache.jsp.admin_home_page_jsp
            out.println("the count: " + count);
                                                      ^

Author

Commented:
sorry i posted the wrong code i was actually using
          int count = res.getInt(1);
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thats great thanks

Commented:
I have written a javascript. Its a form. I need to somehow connect to my sql database and then add all the information into sql tables. I would really appreciate if someone can help me with this part. I am writing the code of the form here just so you have an idea of what information my sql tables need to store. If you just copy paste this file as *.html it should work just to save u some time.Thank you very much for your time.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>The Website made by Raship Shah</TITLE>
<STYLE>.c {
      FONT-SIZE: 12px; COLOR: #0000ff; FONT-FAMILY: Arial,Helvetica
}
.d {
      FONT-SIZE: 12px; COLOR: #000099; FONT-FAMILY: Arial,Helvetica
}
.r {
      FONT-SIZE: 12px; COLOR: #000000; FONT-FAMILY: Arial,Helvetica
}
.info {
      FONT-SIZE: 14px; COLOR: #000000; FONT-FAMILY: Arial,Helvetica
}
.nav {
      FONT-WEIGHT: bold; FONT-SIZE: 15px; COLOR: #ffffb0; FONT-FAMILY: Arial,Helvetica
}
.footer {
      FONT-SIZE: 10px; COLOR: #000099; FONT-FAMILY: Arial,Helvetica
}
.smtxt {
      FONT-SIZE: 10px; COLOR: #000000; FONT-FAMILY: Arial,Helvetica
}
.mdtxt {
      FONT-SIZE: 11px; COLOR: #000000; FONT-FAMILY: Arial,Helvetica
}
.menubar {
      FONT-SIZE: 11px; COLOR: #8080d0; FONT-FAMILY: Arial,Helvetica
}
.phone {
      FONT-WEIGHT: bold; FONT-SIZE: 16px; COLOR: #ffffff
}
.s {
      FONT-SIZE: 13px; COLOR: #000000; FONT-FAMILY: Geneva,Arial,Helvetica
}
.form {
      FONT-SIZE: 11px; COLOR: #000000; FONT-FAMILY: Geneva,Arial,Helvetica
}
</STYLE>

<SCRIPT language=JavaScript1.1 src="FormChek.js">
</SCRIPT>

<SCRIPT>
function validateUSPersonalInfo(form)
{   return (
            checkString(form.elements["last_name"],sUSLastName) &&
            checkString(form.elements["first_name"],sUSFirstName) &&
          checkString(form.elements["contact_number"],'Contact Number') &&
      checkString(form.elements["diners"],'Number of Diners') &&
        checkEmail(form.elements["email"], false)
    )
}
</SCRIPT>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TBODY>
  <TR>
    <TD><BR>
      <FORM action=procpage.html method=post><INPUT type=hidden value=775 name=r>
      <CENTER>
      <TABLE cellPadding=1 width=500 bgColor=#f0f0f0 border=0>
        <TBODY>
        <TR>
          <TD class=form><p>First Name</p>          </TD>
          <TD class=form><INPUT size=30 name=first_name></TD></TR>
        <TR>
          <TD class=form><p>&nbsp;</p>
          <p>Last Name</p></TD>
          <TD class=form><INPUT size=30 name=last_name></TD></TR>
        <TR>
          <TD class=form><p>&nbsp;</p>
          <p>Telephone Number</p></TD>
          <TD class=form><INPUT name=contact_number></TD></TR>
        <TR>
          <TD class=form><p>&nbsp;</p>
          <p>Number of Diners:</p></TD>
          <TD class=form><INPUT size=5 name=diners></TD></TR>
        <TR>
          <TD class=form><p>&nbsp;</p>
          <p>Preferred Reservation Date:</p></TD>
          <TD class=form><SELECT name=month> <OPTION selected
              val="Select month">Select month</OPTION> <OPTION
              val="March">March</OPTION> <OPTION val="April">April</OPTION>
              <OPTION val="May">May</OPTION></SELECT> <SELECT name=date> <OPTION
              val="1">1</OPTION> <OPTION val="2">2</OPTION> <OPTION selected
              val="3">3</OPTION> <OPTION val="4">4</OPTION> <OPTION
              val="5">5</OPTION> <OPTION val="6">6</OPTION> <OPTION
              val="7">7</OPTION> <OPTION val="8">8</OPTION> <OPTION
              val="9">9</OPTION> <OPTION val="10">10</OPTION> <OPTION
              val="11">11</OPTION> <OPTION val="12">12</OPTION> <OPTION
              val="13">13</OPTION> <OPTION val="14">14</OPTION> <OPTION
              val="15">15</OPTION> <OPTION val="16">16</OPTION> <OPTION
              val="17">17</OPTION> <OPTION val="18">18</OPTION> <OPTION
              val="19">19</OPTION> <OPTION val="20">20</OPTION> <OPTION
              val="21">21</OPTION> <OPTION val="22">22</OPTION> <OPTION
              val="23">23</OPTION> <OPTION val="24">24</OPTION> <OPTION
              val="25">25</OPTION> <OPTION val="26">26</OPTION> <OPTION
              val="27">27</OPTION> <OPTION val="28">28</OPTION> <OPTION
              val="29">29</OPTION> <OPTION val="30">30</OPTION> <OPTION
              val="31">31</OPTION></SELECT> <SELECT name=year> <OPTION selected
              val="2004">2004</OPTION></SELECT> &nbsp; <a href="calender.html">(Calendar)</a></TD>
        </TR>
        <TR>
          <TD class=form><p>&nbsp;</p>
          <p>Approximate 1st Choice Reservation Time</p></TD>
          <TD class=form><SELECT name=firsttime><OPTION>1:00</OPTION>
              <OPTION>1:30</OPTION> <OPTION>2:00</OPTION> <OPTION>2:30</OPTION>
              <OPTION>3:00</OPTION> <OPTION>3:30</OPTION> <OPTION>4:00</OPTION>
              <OPTION>4:30</OPTION> <OPTION>5:00</OPTION> <OPTION>5:30</OPTION>
              <OPTION>6:00</OPTION> <OPTION>6:30</OPTION> <OPTION
              selected>7:00</OPTION> <OPTION>7:30</OPTION> <OPTION>8:00</OPTION>
              <OPTION>8:30</OPTION> <OPTION>9:00</OPTION> <OPTION>9:30</OPTION>
              <OPTION>10:00</OPTION> <OPTION>10:30</OPTION>
              <OPTION>11:00</OPTION> <OPTION>11:30</OPTION>
              <OPTION>12:00</OPTION> <OPTION>12:30</OPTION></SELECT> <INPUT
            type=radio value=AM name=firstampm>AM <INPUT type=radio CHECKED
            value=PM name=firstampm>PM </TD></TR>
        <TR>
          <TD class=form><p>&nbsp;</p>
          <p>Approximate 2nd Choice Reservation Time</p></TD>
          <TD class=form><SELECT name=secondtime><OPTION>1:00</OPTION>
              <OPTION>1:30</OPTION> <OPTION>2:00</OPTION> <OPTION>2:30</OPTION>
              <OPTION>3:00</OPTION> <OPTION>3:30</OPTION> <OPTION>4:00</OPTION>
              <OPTION>4:30</OPTION> <OPTION>5:00</OPTION> <OPTION>5:30</OPTION>
              <OPTION>6:00</OPTION> <OPTION>6:30</OPTION> <OPTION>7:00</OPTION>
              <OPTION>7:30</OPTION> <OPTION selected>8:00</OPTION>
              <OPTION>8:30</OPTION> <OPTION>9:00</OPTION> <OPTION>9:30</OPTION>
              <OPTION>10:00</OPTION> <OPTION>10:30</OPTION>
              <OPTION>11:00</OPTION> <OPTION>11:30</OPTION>
              <OPTION>12:00</OPTION> <OPTION>12:30</OPTION></SELECT> <INPUT
            type=radio value=AM name=secondampm>AM <INPUT type=radio CHECKED
            value=PM name=secondampm>PM </TD></TR>
        <TR>
          <TD class=form><p>&nbsp;</p>
          <p>Seating preference:</p></TD>
          <TD class=form><INPUT type=radio CHECKED value=N
            name=smoking>Non-Smoking <INPUT type=radio value=Y
            name=smoking>Smoking <INPUT type=radio value=E name=smoking>Either
          </TD></TR>
        <TR>
          <TD class=form><p>&nbsp;</p>
          <p>Confirm via:</p></TD>
          <TD class=form><p>
              <INPUT type=radio CHECKED value=E
            name=confirm_method>
              E-mail
              </p>
            <p>
    <INPUT type=radio value=P
            name=confirm_method>
    Phone/voicemail    </p>
            <p>
    <INPUT type=radio value=F
            name=confirm_method>
    Fax </p></TD></TR>
        <TR>
          <TD class=form><p>&nbsp;</p>
          <p>E-mail:</p></TD>
          <TD class=form><INPUT size=25 name=email></TD></TR>
        <TR>
          <TD class=form><p>&nbsp;</p>
          <p>Business telephone (w/area code):</p></TD>
          <TD class=form><INPUT name=business_number></TD></TR>
        <TR>
          <TD class=form><p>&nbsp;</p>
          <p>Fax number:</p></TD>
          <TD class=form><INPUT name=fax_number></TD></TR>
          <p><BR>
            <INPUT name=reserve type=submit onclick="return validateUSPersonalInfo(this.form);" value="Submit Reservation Now">
      </BR>
        </p>
   
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.