Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //AlertDao.java the service to insert country name in country table
- public class AlertDao {
- public boolean insertCountry(Connection connection, String countryName) {
- PreparedStatement ps;
- boolean insertStatus = false;
- try {
- ps = connection.prepareStatement("SELECT COUNTRY_ID FROM COUNTRY WHERE COUNTRY_NAME = ?");
- ps.setString(1, countryName);
- ResultSet rs = ps.executeQuery();
- if (!rs.next()) {
- rs.close();
- ps.close();
- ps = connection.prepareStatement("INSERT INTO COUNTRY (COUNTRY_NAME) VALUES (?) ");
- ps.setString(1, countryName);
- int records = ps.executeUpdate();
- if (records > 0) {
- insertStatus = true;
- }
- }
- ps.close();
- connection.commit();
- connection.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- return insertStatus;
- }
- }
- /*************************************************************************************/
- //Database.java for connection with mySQL
- public class Database {
- public Connection Get_Connection() throws Exception
- {
- try
- {
- String connectionURL = "jdbc:mysql://localhost:3306/atm";
- Connection connection = null;
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- connection = DriverManager.getConnection(connectionURL, "root", "admin");
- return connection;
- }
- catch (SQLException e)
- {
- throw e;
- }
- catch (Exception e)
- {
- throw e;
- }
- }
- }
- /**************************************************************************************************/
- //AtmService.java to call service to insert country name in mysql db
- @Path("/WebService")
- public class AtmService {
- @PUT
- @Path("/insertCountry")
- //@PathParam("connection")
- //@PathParam("countryName")
- public int insertCountry(String countryName) {
- System.out.println("Inside checkCredentials");
- int result = 3;
- try {
- Database database = new Database();
- Connection connection = database.Get_Connection();
- AlertDao n = new AlertDao();
- boolean b = n.insertCountry(connection, countryName);
- Gson gson = new Gson();
- System.out.println(gson.toJson(b));
- result = 1;
- } catch (Exception e) {
- System.out.println(e);
- }
- System.out.println(result);
- return result;
- }
- }
- /*******************************************/
- URL which I am passing
- http://localhost:8080/DemoJavaAngular/REST/WebService/insertCountry/"INDIA"
- DemoJavaAngular- Project Name
- REST - URL pattern
- WebService/insertContry - Path name
- "INDIA" - parameter
- My question after passing this URL I am getting no response neither the country name is added nor any kind of exception or erros are genrated.
Add Comment
Please, Sign In to add comment