Web服务器代码无法正常工作
我试图编译一些代码,并使其在通过虚拟机创建的此Web服务索引程序中正常工作。Web服务器代码无法正常工作
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
现在,前两种方法,这是检索所有内容,并检索由ID都正常工作的项目,它是按名称代码,无法检索。当我在我的虚拟机上的cmd上运行它时正确编译,并且在Tomcat 8上没有显示任何错误,但正确给我结果的唯一代码是前两种方法。出于某种原因,第三种方法不断吐出第一个结果,并且只是第一个结果。
我还附上index.html文件代码向你展示什么上面的代码与...
<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>
是否有可以在这里提出来的任何建议的工作,为什么按名称获取方法在我的IngredientServices中javascript无法正常工作?我错过了什么吗?
编辑 - 2014年11月4日 - 16:05 ...
我想,这个问题可能是数据库程序的这一部分...而是通过寻找说按名称搜索的一个组成成分的元素的ID,我应该在给定的参数内搜索NAME。希望这可以解决我遇到的问题...
顺便说一句,这是我修改过的代码:var inputName = document.getElementByName(“t_ingredName”).value;
当我将你的代码的Firefox和点击了所谓的Firebug插件,它给我以下错误:
SyntaxError: missing ; before statement
var theUrl: "http://localhost:8080/webserv1/resources/ws2/ingredients/
因此它应该是var theUrl= "http://localhost:8080/webserv1/resources/ws2/ingredients/ingredient?name="+inputName;
你试过调试?
另外,不是使用警报,而是使用console.log("your message here");
- 它会显示在Firebug的控制台中。
原因是.....代替localhost:8080是因为当我试图输入时,它不会让我。抱歉。然而,我知道一个事实,那就是正确的联系。 – user2891351 2014-11-04 22:17:53
事实证明,我在上面的问题创建代码令人欣慰的正常工作,尽管在一些不幸的错误检索成分的,我的Java代码名称方法......这是最终什么需要一些固定。
当你直接对数据库执行sql命令时,你会得到多个结果吗? – 2014-11-01 20:36:08
如果在对数据库执行sql时只获得一个结果,那意味着数据库中只有一个匹配条目,并且代码按预期工作。 – 2014-11-02 18:00:45
经过一段时间才能更好地了解事情。我按照指示采纳了你的建议。它似乎可能是index.html中的某些东西导致我头痛的问题。 – user2891351 2014-11-04 21:03:09