?
Solved

JSP and MYsql database

Posted on 2003-03-24
6
Medium Priority
?
154 Views
Last Modified: 2010-03-31
The code below is what i have been using to try and get a connection along with some information with the database. The only problem that i can not get passed results = sql.executeQuery(); I have added println statement and i only get up to 3 which is before results = sql.executeQuery(); What is my mistake??

When i run the query, SELECT member_id FROM member WHERE user_name = + '" + name + "'" and hard code the name it does get back one number. So i don't know what the problem is!

Please correct the file below to show me where i have gone wrong..thank-you in advance



<html>
<head>
<%@ page
     import = "java.io.*"
     import = "java.lang.*"
     import = "java.sql.*"
%>
<title>
TEST
</title>
</head>
<body>
<%
     String     name;
     Connection dbconn;
     ResultSet results;
     PreparedStatement sql;
     try
     {
          Class.forName("org.gjt.mm.mysql.Driver").newInstance();
          try
          {
               int mem;
               boolean     doneheading = false;
               out.println("1");
               out.println();
               dbconn = DriverManager.getConnection("jdbc:mysql://localhost/nutrition","saundersk","kevin");
         
               out.println("2");
               name = request.getParameter("name");
               sql = dbconn.prepareStatement("SELECT member_id FROM member WHERE user_name = + '" + name + "'");
               out.println("3");
         
               results = sql.executeQuery();
               
               out.println("4");
               while(results.next())
               {
                    out.println("5");
                    if(! doneheading)
                    {
                         out.println("6");
                         out.println("<table border=2>");
                         doneheading = true;
                         
                    }
                                out.println("7");
                                mem = results.getInt("member_id");
                    out.println("<tr><td>" + mem);
         

               }
               if(doneheading)
               {
                    out.println("8");
                    out.println("</table>");
               }
               else
               {
                    out.println("9");
                    out.println("No matches for ");
               }
          }          
          catch (SQLException s)
          {
               out.println("SQL Error<br>");
          }
     }
     catch (ClassNotFoundException err)
     {
          out.println("Class loading error");
        }
%>
</body>
</html>
0
Comment
Question by:KTS
[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
  • 3
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
sfotex earned 140 total points
ID: 8198983
try:
              sql = dbconn.prepareStatement("SELECT member_id FROM member WHERE user_name =  ? + ");


sql.setString(1, request.getParameter("name"));
results = sql.executeQuery();

0
 
LVL 92

Expert Comment

by:objects
ID: 8199343
> member WHERE user_name = +

Get rid of that '+'
0
 

Author Comment

by:KTS
ID: 8199832
What does sql.setString(1, request.getParameter("name")); this do?


if i wanted to change my SELECT STATEMENT TO..."SELECT * FROM member WHERE UPPER(user_name) = ?"

how would i change the rest of the code to get all the fields output..there are five fields....username, password1, password2, memeber_id, email_add
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 92

Expert Comment

by:objects
ID: 8199868
Did u try just getting rid of the '+'?

> What does sql.setString(1, request.getParameter("name")); this do?

It fills in the value of the 1st placeholder ?

> how would i change the rest of the code to get all the fields output..

Use result.getObject(fieldname) to retrieve the column values from the rs.

0
 

Author Comment

by:KTS
ID: 8203592
What does sql.setString(1, request.getParameter("name")); this do?


if i wanted to change my SELECT STATEMENT TO..."SELECT * FROM member WHERE UPPER(user_name) = ?"

how would i change the rest of the code to get all the fields output..there are five fields....username, password1, password2, memeber_id, email_add
0
 

Author Comment

by:KTS
ID: 8203593
What does sql.setString(1, request.getParameter("name")); this do?


if i wanted to change my SELECT STATEMENT TO..."SELECT * FROM member WHERE UPPER(user_name) = ?"

how would i change the rest of the code to get all the fields output..there are five fields....username, password1, password2, memeber_id, email_add
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

777 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