Advertisement
Guest User

Untitled

a guest
May 12th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.74 KB | None | 0 0
  1. package com.prgguru.com;
  2. public class Constants {
  3. public static String dbClass = "com.mysql.jdbc.Driver";
  4. private static String dbName= "myapp";
  5. public static String dbUrl = "jdbc:mysql://localhost:3306/"+dbName;
  6. public static String dbUser = "root";
  7. public static String dbPwd = "";
  8. }
  9.  
  10. package com.prgguru.com;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16.  
  17. public class DBConnection {
  18. /**
  19. * Method to create DB Connection
  20. *
  21. * @return
  22. * @throws Exception
  23. */
  24. @SuppressWarnings("finally")
  25. public static Connection createConnection() throws Exception {
  26. Connection con = null;
  27. try {
  28. Class.forName(Constants.dbClass);
  29. con = DriverManager.getConnection(Constants.dbUrl, Constants.dbUser, Constants.dbPwd);
  30. } catch (Exception e) {
  31. throw e;
  32. } finally {
  33. return con;
  34. }
  35. }
  36. /**
  37. * Method to check whether uname and pwd combination are correct
  38. *
  39. * @param uname
  40. * @param pwd
  41. * @return
  42. * @throws Exception
  43. */
  44. public static boolean checkLoginStudent(String uname, String pwd) throws Exception {
  45. boolean isUserAvailable = false;
  46. Connection dbConn = null;
  47. try {
  48. try {
  49. dbConn = DBConnection.createConnection();
  50. } catch (Exception e) {
  51. // TODO Auto-generated catch block
  52. e.printStackTrace();
  53. }
  54. Statement stmt = dbConn.createStatement();
  55. String query = "SELECT * FROM student WHERE stuid = '" + uname
  56. + "' AND pass=" + "'" + pwd + "'";
  57. //System.out.println(query);
  58. ResultSet rs = stmt.executeQuery(query);
  59. while (rs.next()) {
  60. //System.out.println(rs.getString(1) + rs.getString(2) + rs.getString(3));
  61. isUserAvailable = true;
  62. }
  63. } catch (SQLException sqle) {
  64. throw sqle;
  65. } catch (Exception e) {
  66. // TODO Auto-generated catch block
  67. if (dbConn != null) {
  68. dbConn.close();
  69. }
  70. throw e;
  71. } finally {
  72. if (dbConn != null) {
  73. dbConn.close();
  74. }
  75. }
  76. return isUserAvailable;
  77. }
  78.  
  79. public static boolean checkLoginTeacher(String uname, String pwd) throws Exception {
  80. boolean isUserAvailable = false;
  81. Connection dbConn = null;
  82. try {
  83. try {
  84. dbConn = DBConnection.createConnection();
  85. } catch (Exception e) {
  86. // TODO Auto-generated catch block
  87. e.printStackTrace();
  88. }
  89. Statement stmt = dbConn.createStatement();
  90. String query = "SELECT * FROM teacher WHERE teacherid = '" + uname
  91. + "' AND pass=" + "'" + pwd + "'";
  92. //System.out.println(query);
  93. ResultSet rs = stmt.executeQuery(query);
  94. while (rs.next()) {
  95. //System.out.println(rs.getString(1) + rs.getString(2) + rs.getString(3));
  96. isUserAvailable = true;
  97. }
  98. } catch (SQLException sqle) {
  99. throw sqle;
  100. } catch (Exception e) {
  101. // TODO Auto-generated catch block
  102. if (dbConn != null) {
  103. dbConn.close();
  104. }
  105. throw e;
  106. } finally {
  107. if (dbConn != null) {
  108. dbConn.close();
  109. }
  110. }
  111. return isUserAvailable;
  112. }
  113. /**
  114. * Method to insert uname and pwd in DB
  115. *
  116. * @param name
  117. * @param uname
  118. * @param pwd
  119. * @return
  120. * @throws SQLException
  121. * @throws Exception
  122. */
  123. public static boolean insertStudent(String stuid,String fname,String lname ,String email ,String pass ,String department ,String stream) throws SQLException, Exception {
  124. boolean insertStatus = false;
  125. Connection dbConn = null;
  126. try {
  127. try {
  128. dbConn = DBConnection.createConnection();
  129. } catch (Exception e) {
  130. // TODO Auto-generated catch block
  131. e.printStackTrace();
  132. }
  133. Statement stmt = dbConn.createStatement();
  134. String query = "INSERT into student(stuid, firstname, lastname, email, mobileno, pass, department, stream, yearofstudy) values('"+stuid+"',"+"'"+fname+"'"+ ", '"+lname+ "'"+", '"+email+ "'"+", '"+""+"'"+", '"+pass+"'"+ ", '"+department+"'"+ ", '"+stream+"'"+", '"+""+"')";
  135. //System.out.println(query);
  136. int records = stmt.executeUpdate(query);
  137. //System.out.println(records);
  138. //When record is successfully inserted
  139. if (records > 0) {
  140. insertStatus = true;
  141. }
  142. } catch (SQLException sqle) {
  143. //sqle.printStackTrace();
  144. throw sqle;
  145. } catch (Exception e) {
  146. //e.printStackTrace();
  147. // TODO Auto-generated catch block
  148. if (dbConn != null) {
  149. dbConn.close();
  150. }
  151. throw e;
  152. } finally {
  153. if (dbConn != null) {
  154. dbConn.close();
  155. }
  156. }
  157. return insertStatus;
  158. }
  159.  
  160.  
  161. public static boolean insertUser(String teacherid,String fname,String lname ,String email ,String pass ,String department) throws SQLException, Exception {
  162. boolean insertStatus = false;
  163. Connection dbConn = null;
  164. try {
  165. try {
  166. dbConn = DBConnection.createConnection();
  167. } catch (Exception e) {
  168. // TODO Auto-generated catch block
  169. e.printStackTrace();
  170. }
  171. Statement stmt = dbConn.createStatement();
  172. String query = "INSERT into teacher(teacherid, firstname, lastname, email, mobileno, pass, department) values('"+teacherid+"',"+"'"+fname+"'"+ ", '"+lname+ "'"+", '"+email+ "'"+", '"+""+"'"+", '"+pass+"'"+ ", '"+department+"')";
  173. //System.out.println(query);
  174. int records = stmt.executeUpdate(query);
  175. //System.out.println(records);
  176. //When record is successfully inserted
  177. if (records > 0) {
  178. insertStatus = true;
  179. }
  180. } catch (SQLException sqle) {
  181. //sqle.printStackTrace();
  182. throw sqle;
  183. } catch (Exception e) {
  184. //e.printStackTrace();
  185. // TODO Auto-generated catch block
  186. if (dbConn != null) {
  187. dbConn.close();
  188. }
  189. throw e;
  190. } finally {
  191. if (dbConn != null) {
  192. dbConn.close();
  193. }
  194. }
  195. return insertStatus;
  196. }
  197.  
  198. package com.prgguru.com;
  199. import java.sql.SQLException;
  200. import javax.ws.rs.GET;
  201. import javax.ws.rs.Path;
  202. import javax.ws.rs.Produces;
  203. import javax.ws.rs.core.MediaType;
  204. import javax.ws.rs.core.UriInfo;
  205. import javax.ws.rs.core.Context;
  206.  
  207. import com.prgguru.com.DBConnection;
  208. import com.prgguru.com.Utility;
  209.  
  210. //Path: http://localhost/<appln-folder-name>/studentregister
  211. @Path("/studentregister")
  212. public class StudentRegister {
  213. // HTTP Get Method
  214. @GET
  215. // Path: http://localhost/<appln-folder-name>/studentregister/doregister
  216. @Path("/doregister")
  217. // Produces JSON as response
  218. @Produces(MediaType.APPLICATION_JSON)
  219. // 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
  220. public String doRegister(@Context UriInfo info){
  221.  
  222. String response = "";
  223. int retCode;
  224.  
  225. String stuid= info.getQueryParameters().getFirst("stuid");
  226. String fname=info.getQueryParameters().getFirst("firstname");
  227. String lname=info.getQueryParameters().getFirst("lastname");
  228. String email=info.getQueryParameters().getFirst("email");
  229. String pass=info.getQueryParameters().getFirst("pass");
  230. String department=info.getQueryParameters().getFirst("department");
  231. String stream="";
  232.  
  233. if(department.equals("BTech")){
  234. stream= info.getQueryParameters().getFirst("stream");
  235. }
  236. retCode = registerStudent(stuid,fname,lname,email,pass,department,stream);
  237.  
  238. //System.out.println("Inside doLogin "+uname+" "+pwd);
  239.  
  240. if(retCode == 0){
  241. response = Utility.constructJSON("register",true);
  242. }else if(retCode == 1){
  243. response = Utility.constructJSON("register",false, "You are already registered");
  244. }else if(retCode == 2){
  245. response = Utility.constructJSON("register",false, "Special Characters are not allowed in Username and Password");
  246. }else if(retCode == 3){
  247. response = Utility.constructJSON("register",false, "Error occured");
  248. }
  249. return response;
  250.  
  251. }
  252.  
  253. private int registerStudent(String stuid, String fname, String lname , String email , String pass , String department , String stream){
  254. System.out.println("Inside checkCredentials");
  255. int result = 3;
  256. if(Utility.isNotNull(stuid) && Utility.isNotNull(fname) && Utility.isNotNull(lname) && Utility.isNotNull(email) && Utility.isNotNull(pass) && Utility.isNotNull(department) && Utility.isNotNull(stream)){
  257. try {
  258. if(DBConnection.insertStudent(stuid, fname, lname ,email ,pass ,department ,stream)){
  259. System.out.println("RegisterStudent if");
  260. result = 0;
  261. }
  262. } catch(SQLException sqle){
  263. System.out.println("RegisterStudent catch sqle");
  264. //When Primary key violation occurs that means user is already registered
  265. if(sqle.getErrorCode() == 1062){
  266. result = 1;
  267. }
  268. //When special characters are used in name,username or password
  269. else if(sqle.getErrorCode() == 1064){
  270. System.out.println(sqle.getErrorCode());
  271. result = 2;
  272. }
  273. }
  274. catch (Exception e) {
  275. // TODO Auto-generated catch block
  276. System.out.println("Inside checkCredentials catch e ");
  277. result = 3;
  278. }
  279. }else{
  280. System.out.println("Inside checkCredentials else");
  281. result = 3;
  282. }
  283.  
  284. return result;
  285. }
  286.  
  287. package com.prgguru.com;
  288.  
  289. import javax.ws.rs.GET;
  290. import javax.ws.rs.Path;
  291. import javax.ws.rs.Produces;
  292. import javax.ws.rs.core.Context;
  293. import javax.ws.rs.core.MediaType;
  294. import javax.ws.rs.core.UriInfo;
  295.  
  296. import com.prgguru.com.DBConnection;
  297. import com.prgguru.com.Utility;
  298.  
  299. //Path: http://localhost/<appln-folder-name>/studentlogin
  300. @Path("/studentlogin")
  301.  
  302. public class StudentLogin {
  303.  
  304. public class Login {
  305. // HTTP Get Method
  306. @GET
  307. // Path: http://localhost/<appln-folder-name>/studentlogin/dologin
  308. @Path("/dologin")
  309. // Produces JSON as response
  310. @Produces(MediaType.APPLICATION_JSON)
  311. // Query parameters are parameters: http://localhost/<appln-folder-name>/login/dologin?username=abc&password=xyz
  312. public String doLogin(@Context UriInfo info){
  313. String username=info.getQueryParameters().getFirst("username");
  314. String password=info.getQueryParameters().getFirst("password");
  315. String response = "";
  316. if(checkCredentials(username,password)){
  317. response = Utility.constructJSON("login",true);
  318. }else{
  319. response = Utility.constructJSON("login", false, "Incorrect UserId or Password");
  320. }
  321. return response;
  322. }
  323.  
  324. /**
  325. * Method to check whether the entered credential is valid
  326. *
  327. * @param uname
  328. * @param pwd
  329. * @return
  330. */
  331. private boolean checkCredentials(String username, String password){
  332. //System.out.println("Inside checkCredentials");
  333. boolean result = false;
  334. if(Utility.isNotNull(username) && Utility.isNotNull(password)){
  335. try {
  336. result = DBConnection.checkLoginStudent(username, password);
  337. }
  338. //System.out.println("Inside checkCredentials try "+result);
  339. catch (Exception e) {
  340. // TODO Auto-generated catch block
  341. //System.out.println("Inside checkCredentials catch");
  342. result = false;
  343. }
  344. }
  345. else{
  346. //System.out.println("Inside checkCredentials else");
  347. result = false;
  348. }
  349.  
  350. return result;
  351. }
  352.  
  353. }
  354.  
  355. package com.prgguru.com;
  356.  
  357. import org.codehaus.jettison.json.JSONException;
  358. import org.codehaus.jettison.json.JSONObject;
  359.  
  360. public class Utility {
  361. /**
  362. * Null check Method
  363. *
  364. * @param txt
  365. * @return
  366. */
  367. public static boolean isNotNull(String txt) {
  368. // System.out.println("Inside isNotNull");
  369. return txt != null && txt.trim().length() >= 0 ? true : false;
  370. }
  371.  
  372. /**
  373. * Method to construct JSON
  374. *
  375. * @param tag
  376. * @param status
  377. * @return
  378. */
  379. public static String constructJSON(String tag, boolean status) {
  380. JSONObject obj = new JSONObject();
  381. try {
  382. obj.put("tag", tag);
  383. obj.put("status", new Boolean(status));
  384. } catch (JSONException e) {
  385. // TODO Auto-generated catch block
  386. }
  387. return obj.toString();
  388. }
  389.  
  390. /**
  391. * Method to construct JSON with Error Msg
  392. *
  393. * @param tag
  394. * @param status
  395. * @param err_msg
  396. * @return
  397. */
  398. public static String constructJSON(String tag, boolean status,String err_msg) {
  399. JSONObject obj = new JSONObject();
  400. try {
  401. obj.put("tag", tag);
  402. obj.put("status", new Boolean(status));
  403. obj.put("error_msg", err_msg);
  404. } catch (JSONException e) {
  405. // TODO Auto-generated catch block
  406. }
  407. return obj.toString();
  408. }
  409.  
  410. ?xml version="1.0" encoding="UTF-8"?>
  411. <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">
  412. <display-name>Engine</display-name>
  413.  
  414. <welcome-file-list>
  415. <welcome-file>index.html</welcome-file>
  416. <welcome-file>index.htm</welcome-file>
  417. <welcome-file>index.jsp</welcome-file>
  418. <welcome-file>default.html</welcome-file>
  419. <welcome-file>default.htm</welcome-file>
  420. <welcome-file>default.jsp</welcome-file>
  421. </welcome-file-list>
  422.  
  423.  
  424. <servlet>
  425. <servlet-name>Jersey REST Service</servlet-name>
  426. <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  427. <init-param>
  428. <param-name>com.sun.jersey.config.property.packages</param-name>
  429. <param-value>com.prgguru.com</param-value>
  430. </init-param>
  431. <load-on-startup>1</load-on-startup>
  432. </servlet>
  433.  
  434. <servlet-mapping>
  435. <servlet-name>Jersey REST Service</servlet-name>
  436. <url-pattern>/*</url-pattern>
  437. </servlet-mapping>
  438.  
  439.  
  440. </web-app>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement