Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String getIngredients() throws SQLException, ClassNotFoundException {
- 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 ");
- ResultSet rs = ps.executeQuery();
- String result = "";
- while(rs.next()){
- int theId = rs.getInt("id");
- String theName = rs.getString("name");
- String theCat = rs.getString("category");
- result += "id: "+theId+",name:"+theName+"("+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 ps1 = con.prepareStatement("SELECT id, name, category FROM ingredient WHERE id=?");
- ps1.setInt(1, intId);
- ResultSet rs = ps1.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;
- }
- public String getIngredientByName(@QueryParam("name") String theName) throws SQLException, ClassNotFoundException {
- String theIng = "";
- 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 ps2 = con.prepareStatement("SELECT id, name, category FROM ingredient WHERE name=?");
- ps2.setString(1, theName);
- ResultSet rs = ps2.executeQuery();
- String result ="";
- while(rs.next()){
- int theld = rs.getInt("id");
- String theName2 = rs.getString("name");
- String theCategory = rs.getString("category");
- result += "id: "+theld+",name:"+theName2+"("+theCategory +")"+"\n";
- }
- return result;
- }
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement