user2891351 user2891351 - 6 months ago 12
SQL Question

Web server code not working properly

I'm trying to compile some code and get it to work properly in this web service index program that I have created, via a virtual machine.

package com.cs330;
import javax.ws.rs.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Path("ws2")
public class IngredientServices
{
@Path("/ingredients")
@GET
@Produces("text/plain")
public String getIngredients() throws SQLException, ClassNotFoundException {

String connectStr="jdbc:mysql://localhost:3306/fooddb";
//database username

String username="root";
//database password

String password="csci330pass";
/* The driver is the Java class used for accessing
* a particular database. You must download this from
* the database vendor.
*/

String driver="com.mysql.jdbc.Driver";
Class.forName(driver);
//Creates a connection object for your database

Connection con = DriverManager.getConnection(connectStr, username, password);
/* Creates a statement object to be executed on
* the attached database.
*/

Statement stmt = con.createStatement();
/* Executes a database query and returns the results
* as a ResultSet object.
*/

ResultSet rs = stmt.executeQuery("SELECT id, name, category FROM ingredient");
/* This snippet shows how to parse a ResultSet object.
* Basically, you loop through the object sort of like
* a linkedlist, and use the getX methods to get data
* from the current row. Each time you call rs.next()
* it advances to the next row returned.
* The result variable is just used to compile all the
* data into one string.
*/

String result = "";
while (rs.next())
{
int theId = rs.getInt("id");
String theName = rs.getString("name");
String theCategory = rs.getString("category");
result += "id: "+theId+ " , name: "+theName + "("+theCategory+")" + "\n" + "\n";
}
return result;
}//END METHOD

@Path("/ingredients/{id}")
@GET
@Produces("text/plain")
public String getIngredientById(@PathParam("id") String theId)
throws SQLException, ClassNotFoundException {
int intId = 0;
try
{
intId = Integer.parseInt(theId);
}
catch (NumberFormatException FAIL)
{
intId = 1;
}//Obtaining an ingredient from the database

String connectStr="jdbc:mysql://localhost:3306/fooddb";
String username="root";
String password="csci330pass";
String driver="com.mysql.jdbc.Driver";
Class.forName(driver);
Connection con = DriverManager.getConnection(connectStr, username, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, name, category FROM ingredient
WHERE id=" +intId);

String result = "";
while (rs.next())
{
int theId2 = rs.getInt("id");
String theName2 = rs.getString("name");
String theCategory = rs.getString("category");
result += "id: "+theId2+ " , name: "+theName2 + "("+theCategory+")" + "\n" + "\n";
}
return result;
}//END METHOD

@Path("/ingredients/name")
@GET
@Produces("text/plain")
public String getIngredientByName(@QueryParam("name") String theName)
throws SQLException, ClassNotFoundException
{
//Obtaining an ingredient from the database
String connectStr="jdbc:mysql://localhost:3306/fooddb";
String username="root";
String password="csci330pass";
String driver="com.mysql.jdbc.Driver";
Class.forName(driver);
Connection con = DriverManager.getConnection(connectStr, username, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, name, category FROM ingredient WHERE
name='" + theName + "'");

String result = "";
while (rs.next())
{
int theId3 = rs.getInt("id");
String theName3 = rs.getString("name");
String theCategory = rs.getString("category");
result += "id: "+theId3+ " , name: "+theName3 + "("+theCategory+")" + "\n" + "\n";
}
return result;
}//END METHOD
}//END CODE


Now, the first two methods, which are to retrieve everything and to retrieve items by ID are working properly, it's by retrieve by NAME code that isn't. While it is compiling correctly when I run it on cmd on my virtual machine and not showing any errors on Tomcat 8, The only code that is properly giving me results are the first two methods. For some reason, the third method keeps spitting out the first result and only the first result.

I have also attached the index.html file code to show you what the code above works with...

<html>
<head>
<title>Shakur (S-3) Burton's Web Services</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready( function() {
alert("running script");
$("#btnAll").click(function() {
alert("clicked");
$.ajax( {
url:"http://localhost:8080/webserv1/resources/ws2/ingredients/",
type: "GET",
dataType: "text",
success: function(result) {
alert("success");
$("#p_retrieveAll").html(result); },
error:function(xhr) {
alert("error");
$("#p_retrieveAll").html("Error:"+xhr.status + " " + xhr.statusText);}
} );
});
$("#btnOneId").click(function() {
alert("clicked");
var inputId=document.getElementById("t_ingredId").value;
var theUrl = "http://localhost:8080/webserv1/resources/ws2/ingredients/"+inputId;
$.ajax( {
url: theUrl,
type: "GET",
dataType: "text",
success: function(result) {
alert("success");
$("#p_retrieveOneId").html(result); },
error:function(xhr) {
alert("error");
$("#p_retrieveOneId").html("Error:"+xhr.status+" "+xhr.statusText);}
} );
});
$("#btnOneName").click(function() {
alert("clicked");
var inputName=document.getElementByName("t_ingredName").value;
var theUrl: "http://localhost:8080/webserv1/resources/ws2/ingredients/ingredient?name="+inputName;
$.ajax( {
url: theUrl,
type: "GET",
dataType: "text",
success: function(result) {
alert("success");
$("#p_retrieveOneName").html(result); },
error:function(xhr) {
alert("error");
$("#p_retrieveOneName").html("Error:"+xhr.status+" "+xhr.statusText);}
} );
});
});
</script>
</head>
<body>
<h3>Testing Web Services</h3>
<div id="retrieveAll">
<button id="btnAll">Click to Retrieve All</button>
<p id="p_retrieveAll">Ingredients List Goes here</p>
</div>
<div id="retrieveOneId">
<input type="text" id="t_ingredId" value="type id here" />
<button id="btnOneId">Click to Retrieve by Id</button>
<p id="p_retrieveOneId">Ingredient By Id Goes here</p>
</div>
<div id="retrieveOneName">
<input type="text" id="t_ingredName" value="type name here"/>
<button id="btnOneName">Click to Retrieve by Name</button>
<p id="p_retrieveOneName">Ingredient By Name Goes here</p>
</div>
</body>
</html>


Are there any suggestions that can be offered here as to why the GET by NAME method in my IngredientServices javascript isn't working properly? Am I missing something?

EDIT - 11/4/2014 - 16:05...

I figured that this problem might be in this part of the database program... Instead of searching for an ingredient by name by finding said element by ID, I should search within given parameters for it by NAME. Hopefully, this fixes the problem I was having...

BTW, this is the previous code I have modified: var inputName=document.getElementByName("t_ingredName").value;

Answer

When I added your code to the Firefox and Clicked on the Add-in called Firebug, it showed me the following error:

SyntaxError: missing ; before statement
var theUrl: "http://localhost:8080/webserv1/resources/ws2/ingredients/

Therefore it should be var theUrl= "http://localhost:8080/webserv1/resources/ws2/ingredients/ingredient?name="+inputName;

Have you tried debugging?

Also, instead of using alerts, use console.log("your message here"); - it will show up in the console in Firebug.

Comments