我目前正在处理两个课程,即成分和成分服务。我没有编译Ingredient类的问题但我似乎无法编译另一个而不会在我尝试创建Ingredient对象的行中出现错误,即使它们位于相同的包/目录中。 它们都位于:
C:\ progs的\ COM \ CS330 \
IngredientServices.java
package com.cs330;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.util.Arrays;
@Path("ws2")
public class IngredientServices {
@Path("/ingredients")
@GET
@Produces("text/plain")
public String getIngredients() throws SQLException, ClassNotFoundException {
String connectStr="jdbc:mysql://localhost:3306/fooddb";
String username="*****";
String password="*****";
String driver="com.mysql.jdbc.Driver";
Class.forName(driver);
Connection con = DriverManager.getConnection(connectStr, username, password);
PreparedStatement preStatement = con.prepareStatement("SELECT id, name, category FROM ingredient");
ResultSet rs= preStatement.executeQuery();
Ingredient ing = new Ingredient();
Ingredient[] ingArray = new Ingredient[100];
int i=0;
while (rs.next()) {
ing = new Ingredient(rs.getInt("id"),rs.getString("name"),rs.getString("category"));
ingArray[i] = ing;
i++;
}
ingArray = Arrays.copyOf(ingArray,i);
return Arrays.toString(ingArray);
}
成分类:
package com.cs330;
public class Ingredient {
private int id;
private String name;
private String category;
public Ingredient() {
id = 0;
name = null;
category = null;
}
public Ingredient(int id, String name, String category){
this.id = id;
this.name = name;
this.category = category;
}
public Ingredient(String name, String category){
id = 0;
this.name = name;
this.category = category;
}
public String toString(){
return (id + ": " + name + "( " + category + ") ");
}
}