Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.prgguru.com;
- public class Constants {
- public static String dbClass = "com.mysql.jdbc.Driver";
- private static String dbName= "myapp";
- public static String dbUrl = "jdbc:mysql://localhost:3306/"+dbName;
- public static String dbUser = "root";
- public static String dbPwd = "";
- }
- package com.prgguru.com;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class DBConnection {
- /**
- * Method to create DB Connection
- *
- * @return
- * @throws Exception
- */
- @SuppressWarnings("finally")
- public static Connection createConnection() throws Exception {
- Connection con = null;
- try {
- Class.forName(Constants.dbClass);
- con = DriverManager.getConnection(Constants.dbUrl, Constants.dbUser, Constants.dbPwd);
- } catch (Exception e) {
- throw e;
- } finally {
- return con;
- }
- }
- /**
- * Method to check whether uname and pwd combination are correct
- *
- * @param uname
- * @param pwd
- * @return
- * @throws Exception
- */
- public static boolean checkLoginStudent(String uname, String pwd) throws Exception {
- boolean isUserAvailable = false;
- Connection dbConn = null;
- try {
- try {
- dbConn = DBConnection.createConnection();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Statement stmt = dbConn.createStatement();
- String query = "SELECT * FROM student WHERE stuid = '" + uname
- + "' AND pass=" + "'" + pwd + "'";
- //System.out.println(query);
- ResultSet rs = stmt.executeQuery(query);
- while (rs.next()) {
- //System.out.println(rs.getString(1) + rs.getString(2) + rs.getString(3));
- isUserAvailable = true;
- }
- } catch (SQLException sqle) {
- throw sqle;
- } catch (Exception e) {
- // TODO Auto-generated catch block
- if (dbConn != null) {
- dbConn.close();
- }
- throw e;
- } finally {
- if (dbConn != null) {
- dbConn.close();
- }
- }
- return isUserAvailable;
- }
- public static boolean checkLoginTeacher(String uname, String pwd) throws Exception {
- boolean isUserAvailable = false;
- Connection dbConn = null;
- try {
- try {
- dbConn = DBConnection.createConnection();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Statement stmt = dbConn.createStatement();
- String query = "SELECT * FROM teacher WHERE teacherid = '" + uname
- + "' AND pass=" + "'" + pwd + "'";
- //System.out.println(query);
- ResultSet rs = stmt.executeQuery(query);
- while (rs.next()) {
- //System.out.println(rs.getString(1) + rs.getString(2) + rs.getString(3));
- isUserAvailable = true;
- }
- } catch (SQLException sqle) {
- throw sqle;
- } catch (Exception e) {
- // TODO Auto-generated catch block
- if (dbConn != null) {
- dbConn.close();
- }
- throw e;
- } finally {
- if (dbConn != null) {
- dbConn.close();
- }
- }
- return isUserAvailable;
- }
- /**
- * Method to insert uname and pwd in DB
- *
- * @param name
- * @param uname
- * @param pwd
- * @return
- * @throws SQLException
- * @throws Exception
- */
- public static boolean insertStudent(String stuid,String fname,String lname ,String email ,String pass ,String department ,String stream) throws SQLException, Exception {
- boolean insertStatus = false;
- Connection dbConn = null;
- try {
- try {
- dbConn = DBConnection.createConnection();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Statement stmt = dbConn.createStatement();
- String query = "INSERT into student(stuid, firstname, lastname, email, mobileno, pass, department, stream, yearofstudy) values('"+stuid+"',"+"'"+fname+"'"+ ", '"+lname+ "'"+", '"+email+ "'"+", '"+""+"'"+", '"+pass+"'"+ ", '"+department+"'"+ ", '"+stream+"'"+", '"+""+"')";
- //System.out.println(query);
- int records = stmt.executeUpdate(query);
- //System.out.println(records);
- //When record is successfully inserted
- if (records > 0) {
- insertStatus = true;
- }
- } catch (SQLException sqle) {
- //sqle.printStackTrace();
- throw sqle;
- } catch (Exception e) {
- //e.printStackTrace();
- // TODO Auto-generated catch block
- if (dbConn != null) {
- dbConn.close();
- }
- throw e;
- } finally {
- if (dbConn != null) {
- dbConn.close();
- }
- }
- return insertStatus;
- }
- public static boolean insertUser(String teacherid,String fname,String lname ,String email ,String pass ,String department) throws SQLException, Exception {
- boolean insertStatus = false;
- Connection dbConn = null;
- try {
- try {
- dbConn = DBConnection.createConnection();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Statement stmt = dbConn.createStatement();
- String query = "INSERT into teacher(teacherid, firstname, lastname, email, mobileno, pass, department) values('"+teacherid+"',"+"'"+fname+"'"+ ", '"+lname+ "'"+", '"+email+ "'"+", '"+""+"'"+", '"+pass+"'"+ ", '"+department+"')";
- //System.out.println(query);
- int records = stmt.executeUpdate(query);
- //System.out.println(records);
- //When record is successfully inserted
- if (records > 0) {
- insertStatus = true;
- }
- } catch (SQLException sqle) {
- //sqle.printStackTrace();
- throw sqle;
- } catch (Exception e) {
- //e.printStackTrace();
- // TODO Auto-generated catch block
- if (dbConn != null) {
- dbConn.close();
- }
- throw e;
- } finally {
- if (dbConn != null) {
- dbConn.close();
- }
- }
- return insertStatus;
- }
- package com.prgguru.com;
- import java.sql.SQLException;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.Produces;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.UriInfo;
- import javax.ws.rs.core.Context;
- import com.prgguru.com.DBConnection;
- import com.prgguru.com.Utility;
- //Path: http://localhost/<appln-folder-name>/studentregister
- @Path("/studentregister")
- public class StudentRegister {
- // HTTP Get Method
- @GET
- // Path: http://localhost/<appln-folder-name>/studentregister/doregister
- @Path("/doregister")
- // Produces JSON as response
- @Produces(MediaType.APPLICATION_JSON)
- // Query parameters are parameters: http://localhost/<appln-folder-name>/studentregister/doregister?stuid=abc&firstname=def&lastname=ghi&email=jkl&pass=mno&department=pq&stream=rs
- public String doRegister(@Context UriInfo info){
- String response = "";
- int retCode;
- String stuid= info.getQueryParameters().getFirst("stuid");
- String fname=info.getQueryParameters().getFirst("firstname");
- String lname=info.getQueryParameters().getFirst("lastname");
- String email=info.getQueryParameters().getFirst("email");
- String pass=info.getQueryParameters().getFirst("pass");
- String department=info.getQueryParameters().getFirst("department");
- String stream="";
- if(department.equals("BTech")){
- stream= info.getQueryParameters().getFirst("stream");
- }
- retCode = registerStudent(stuid,fname,lname,email,pass,department,stream);
- //System.out.println("Inside doLogin "+uname+" "+pwd);
- if(retCode == 0){
- response = Utility.constructJSON("register",true);
- }else if(retCode == 1){
- response = Utility.constructJSON("register",false, "You are already registered");
- }else if(retCode == 2){
- response = Utility.constructJSON("register",false, "Special Characters are not allowed in Username and Password");
- }else if(retCode == 3){
- response = Utility.constructJSON("register",false, "Error occured");
- }
- return response;
- }
- private int registerStudent(String stuid, String fname, String lname , String email , String pass , String department , String stream){
- System.out.println("Inside checkCredentials");
- int result = 3;
- if(Utility.isNotNull(stuid) && Utility.isNotNull(fname) && Utility.isNotNull(lname) && Utility.isNotNull(email) && Utility.isNotNull(pass) && Utility.isNotNull(department) && Utility.isNotNull(stream)){
- try {
- if(DBConnection.insertStudent(stuid, fname, lname ,email ,pass ,department ,stream)){
- System.out.println("RegisterStudent if");
- result = 0;
- }
- } catch(SQLException sqle){
- System.out.println("RegisterStudent catch sqle");
- //When Primary key violation occurs that means user is already registered
- if(sqle.getErrorCode() == 1062){
- result = 1;
- }
- //When special characters are used in name,username or password
- else if(sqle.getErrorCode() == 1064){
- System.out.println(sqle.getErrorCode());
- result = 2;
- }
- }
- catch (Exception e) {
- // TODO Auto-generated catch block
- System.out.println("Inside checkCredentials catch e ");
- result = 3;
- }
- }else{
- System.out.println("Inside checkCredentials else");
- result = 3;
- }
- return result;
- }
- package com.prgguru.com;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.Produces;
- import javax.ws.rs.core.Context;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.UriInfo;
- import com.prgguru.com.DBConnection;
- import com.prgguru.com.Utility;
- //Path: http://localhost/<appln-folder-name>/studentlogin
- @Path("/studentlogin")
- public class StudentLogin {
- public class Login {
- // HTTP Get Method
- @GET
- // Path: http://localhost/<appln-folder-name>/studentlogin/dologin
- @Path("/dologin")
- // Produces JSON as response
- @Produces(MediaType.APPLICATION_JSON)
- // Query parameters are parameters: http://localhost/<appln-folder-name>/login/dologin?username=abc&password=xyz
- public String doLogin(@Context UriInfo info){
- String username=info.getQueryParameters().getFirst("username");
- String password=info.getQueryParameters().getFirst("password");
- String response = "";
- if(checkCredentials(username,password)){
- response = Utility.constructJSON("login",true);
- }else{
- response = Utility.constructJSON("login", false, "Incorrect UserId or Password");
- }
- return response;
- }
- /**
- * Method to check whether the entered credential is valid
- *
- * @param uname
- * @param pwd
- * @return
- */
- private boolean checkCredentials(String username, String password){
- //System.out.println("Inside checkCredentials");
- boolean result = false;
- if(Utility.isNotNull(username) && Utility.isNotNull(password)){
- try {
- result = DBConnection.checkLoginStudent(username, password);
- }
- //System.out.println("Inside checkCredentials try "+result);
- catch (Exception e) {
- // TODO Auto-generated catch block
- //System.out.println("Inside checkCredentials catch");
- result = false;
- }
- }
- else{
- //System.out.println("Inside checkCredentials else");
- result = false;
- }
- return result;
- }
- }
- package com.prgguru.com;
- import org.codehaus.jettison.json.JSONException;
- import org.codehaus.jettison.json.JSONObject;
- public class Utility {
- /**
- * Null check Method
- *
- * @param txt
- * @return
- */
- public static boolean isNotNull(String txt) {
- // System.out.println("Inside isNotNull");
- return txt != null && txt.trim().length() >= 0 ? true : false;
- }
- /**
- * Method to construct JSON
- *
- * @param tag
- * @param status
- * @return
- */
- public static String constructJSON(String tag, boolean status) {
- JSONObject obj = new JSONObject();
- try {
- obj.put("tag", tag);
- obj.put("status", new Boolean(status));
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- }
- return obj.toString();
- }
- /**
- * Method to construct JSON with Error Msg
- *
- * @param tag
- * @param status
- * @param err_msg
- * @return
- */
- public static String constructJSON(String tag, boolean status,String err_msg) {
- JSONObject obj = new JSONObject();
- try {
- obj.put("tag", tag);
- obj.put("status", new Boolean(status));
- obj.put("error_msg", err_msg);
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- }
- return obj.toString();
- }
- ?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
- <display-name>Engine</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>Jersey REST Service</servlet-name>
- <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
- <init-param>
- <param-name>com.sun.jersey.config.property.packages</param-name>
- <param-value>com.prgguru.com</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Jersey REST Service</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
- </web-app>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement