Advertisement
Guest User

Untitled

a guest
May 1st, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@page import="java.sql.*"%>
  4. <%@page import="org.json.simple.*"%>
  5. <%@page import="java.util.ArrayList"%>
  6.  
  7.  
  8. <%!String host = "jdbc:mysql://localhost:3306/~~~DB이름?useUnicode = true&characterEncoding=utf8";
  9. String server_user = "~~";
  10. String pw = "~~";
  11. Connection conn;
  12. PreparedStatement pstmt;
  13. String result;
  14. %>
  15.  
  16. <%!
  17. public int searchGcmKey(int userNo){
  18. String sql = "select * from Gcm where User_No ="+userNo;
  19. try {
  20. pstmt = conn.prepareStatement(sql);
  21. ResultSet rs = pstmt.executeQuery(sql);
  22. if(rs.next()){
  23. rs.next();
  24. return -1; //값이 있다면 수정
  25. }else{
  26. return 1; // 값이 없다면 추가
  27. }
  28. } catch (Exception e) {
  29. return 0;//예외
  30. }
  31. }
  32.  
  33. public void updateGcmKey(int userNo, String gcmKey) {
  34. String sql = "update Gcm set Gcm_Key ='"+gcmKey+"'where User_No="+userNo;
  35. try {
  36. pstmt = conn.prepareStatement(sql);
  37. pstmt.executeUpdate();
  38. pstmt.close();
  39. } catch (Exception e) {
  40. //out.println("comment write error " + e.toString());
  41. }
  42. }
  43.  
  44. public void setGcmKey(int userNo,String gcmKey){
  45. try{
  46. Class.forName("com.mysql.jdbc.Driver");
  47. Connection conn = DriverManager.getConnection(host,server_user,pw);
  48. String sql = "insert into Gcm(User_No,Gcm_Key) values(?,?)";
  49. PreparedStatement pstmt = conn.prepareStatement(sql);
  50. pstmt.setInt(1, userNo);
  51. pstmt.setString(2, gcmKey);
  52. pstmt.executeUpdate();
  53. pstmt.close();
  54. }catch(Exception e){
  55. result = e.getMessage();
  56. }
  57. }
  58.  
  59. %>
  60.  
  61. <%
  62. request.setCharacterEncoding("utf-8");
  63. int userNo = Integer.parseInt(request.getParameter("userNo"));
  64. String gcmkey = request.getParameter("gcmKey");
  65. int search;
  66. try {
  67. Class.forName("com.mysql.jdbc.Driver");
  68. conn = DriverManager.getConnection(host, server_user, pw);
  69. } catch (Exception e) {
  70. out.println(e.getMessage());
  71. }
  72. search =searchGcmKey(userNo);
  73. if(search ==1)
  74. setGcmKey(userNo,gcmkey);
  75. else
  76. updateGcmKey(userNo,gcmkey);
  77. %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement