Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class IngredientServices{
- @Path("/ingredients")
- @GET
- @Produces("text/plain")
- public String getIngredients() throws SQLException, ClassNotFoundException {
- String result = "";
- 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);
- PreparedStatement ps = con.prepareStatement("SELECT id, name, category FROM ingredient WHERE id=?");
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- int theId = rs.getInt("id");
- String theName = rs.getString("name");
- String theCat = rs.getString("category");
- System.out.println("id: "+ theId + "Name: " + theName + "Category: " + theCat +
- "\n");
- }
- return result;
- }
- @Path("/ingredients/{id}")
- @GET
- @Produces("text/plain")
- public String getIngredientById(@PathParam("id") String theId) throws SQLException,
- ClassNotFoundException {
- int intId = 0;
- String theIng = "";
- try {
- intId = Integer.parseInt(theId);
- }catch(NumberFormatException ne) {
- intId = 1;
- }
- 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);
- PreparedStatement ps = con.prepareStatement("SELECT id, name, category FROM ingredient
- WHERE id=?");
- ps.setInt(1, intId);
- ResultSet rs = ps.executeQuery();
- String result ="";
- while(rs.next()){
- int theId2 = rs.getInt("id");
- String theName = rs.getString("name");
- String theCategory = rs.getString("category");
- result += "id: "+theId2+",name:"+theName+"("+theCategory
- +")"+"\n";
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement