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

x
?
Solved

JSP and MYsql database

Posted on 2003-03-24
6
Medium Priority
?
155 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
  • 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
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.

 
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

578 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