Advertisement
Guest User

Untitled

a guest
Sep 16th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.57 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package web;
  6.  
  7. import diaryclasses.DiaryRecord;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.Date;
  15. import java.util.List;
  16. import net.sourceforge.stripes.action.ActionBean;
  17. import net.sourceforge.stripes.action.ActionBeanContext;
  18. import net.sourceforge.stripes.action.DefaultHandler;
  19. import net.sourceforge.stripes.action.ForwardResolution;
  20. import net.sourceforge.stripes.action.Resolution;
  21. import net.sourceforge.stripes.validation.Validate;
  22. import org.stripesstuff.plugin.session.Session;
  23.  
  24. /**
  25. *
  26. * @author Satik
  27. */
  28. public class UserActionBean implements ActionBean{
  29.  
  30. ActionBeanContext context;
  31.  
  32. @Override
  33. public void setContext(ActionBeanContext context) {
  34. this.context = context;
  35. }
  36.  
  37. @Override
  38. public ActionBeanContext getContext() {
  39. return this.context;
  40. }
  41.  
  42. @Session
  43. @Validate(required=true, minlength=3, maxlength=50)
  44. private String login;
  45. @Session
  46. @Validate(required=true, minlength=3, maxlength=50)
  47. private String pass;
  48. @Session
  49. @Validate(required=true, minlength=3, maxlength=100)
  50. private String mail;
  51. @Session
  52. @Validate(required=true, minlength=3, maxlength=100)
  53. private String name;
  54. @Session
  55. private int id;
  56.  
  57.  
  58. @Session
  59. private String test;
  60.  
  61. public void setTest(String test) {
  62. this.test = test;
  63. }
  64.  
  65. public String getTest() {
  66. return test;
  67. }
  68.  
  69. @Session
  70. private List<DiaryRecord> entries = new ArrayList<DiaryRecord>();
  71.  
  72. @DefaultHandler
  73. public Resolution Login()
  74. {
  75. name = login;
  76. if (LoadFromDb(login, pass))
  77. {
  78. int count = LoadDiary(); // do count vrati pocet zaznamu v diari
  79. // pokud se povede login - resolution
  80. }
  81. else
  82. {
  83. // pokud se login nepovede - resolution
  84. }
  85. return new ForwardResolution("index.jsp");
  86. }
  87.  
  88. private Boolean LoadFromDb(String login, String password)
  89. {
  90. Boolean loginFound = false;
  91. try
  92. {
  93. Statement stmt;
  94. Class.forName("com.mysql.jdbc.Driver");
  95. String url = "jdbc:mysql://localhost:3306/diary";
  96. Connection con = DriverManager.getConnection(url,"root", "");
  97.  
  98. System.out.println("URL: " + url); // debug
  99. System.out.println("Connection: " + con);
  100.  
  101. stmt = con.createStatement();
  102. //stmt.executeUpdate("CREATE DATABASE JunkDB");
  103.  
  104. ResultSet res = stmt.executeQuery("SELECT * FROM user WHERE login='"+login+"' AND pass='"+password+"'");
  105.  
  106. while (res.next())
  107. {
  108. this.id = res.getInt("id");
  109. this.login = res.getString("login");
  110. this.pass = res.getString("pass");
  111. this.mail = res.getString("mail");
  112. this.name = res.getString("name");
  113.  
  114. loginFound = true;
  115. }
  116.  
  117. con.close();
  118. }
  119. catch( Exception e )
  120. {
  121. System.out.println(e.getStackTrace()); // chyba
  122. return false;
  123. }
  124. return loginFound;
  125. }
  126.  
  127. private int LoadDiary()
  128. {
  129. int count = 0;
  130. try
  131. {
  132. Statement stmt;
  133. Class.forName("com.mysql.jdbc.Driver");
  134. String url = "jdbc:mysql://localhost:3306/diary";
  135. Connection con = DriverManager.getConnection(url,"root", "");
  136.  
  137. System.out.println("URL: " + url); // debug
  138. System.out.println("Connection: " + con);
  139.  
  140. stmt = con.createStatement();
  141. //stmt.executeUpdate("CREATE DATABASE JunkDB");
  142.  
  143. ResultSet res = stmt.executeQuery("SELECT * FROM diaryrecord WHERE iduser='"+this.id+"'");
  144.  
  145. while (res.next())
  146. {
  147. DiaryRecord dr = new DiaryRecord(res.getInt("id"), res.getString("caption"), res.getString("description"), res.getDate("started"), res.getDate("finished"), res.getInt("iduser"));
  148.  
  149. this.entries.add(dr);
  150.  
  151. count ++;
  152. }
  153.  
  154. con.close();
  155. }
  156. catch( Exception e )
  157. {
  158. System.out.println(e.getStackTrace()); // chyba
  159. }
  160. return count;
  161. }
  162.  
  163.  
  164. /*public UserActionBean(int id, String login, String pass, String mail, String name) {
  165. this.id = id;
  166. this.login = login;
  167. this.pass = pass;
  168. this.mail = mail;
  169. this.name = name;
  170. }*/
  171.  
  172. public int getId() {
  173. return id;
  174. }
  175.  
  176. public void setEntries(List<DiaryRecord> entries) {
  177. this.entries = entries;
  178. }
  179.  
  180. public void setId(int id) {
  181. this.id = id;
  182. }
  183.  
  184. public void setLogin(String login) {
  185. this.login = login;
  186. }
  187.  
  188. public void setName(String name) {
  189. this.name = name;
  190. }
  191.  
  192. public List<DiaryRecord> getEntries() {
  193. return entries;
  194. }
  195.  
  196. public String getLogin() {
  197. return login;
  198. }
  199.  
  200. public String getMail() {
  201. return mail;
  202. }
  203.  
  204. public String getName() {
  205. return name;
  206. }
  207.  
  208. public String getPass() {
  209. return pass;
  210. }
  211.  
  212. public void setMail(String mail) {
  213. this.mail = mail;
  214.  
  215. try
  216. {
  217. Statement stmt;
  218. Class.forName("com.mysql.jdbc.Driver");
  219. String url = "jdbc:mysql://localhost:3306/diary";
  220. Connection con = DriverManager.getConnection(url,"root", "");
  221.  
  222. System.out.println("URL: " + url); // debug
  223. System.out.println("Connection: " + con);
  224.  
  225. stmt = con.createStatement();
  226.  
  227. stmt.executeUpdate("UPDATE user SET mail = '"+mail+"' WHERE id='"+this.id+"'");
  228.  
  229. con.close();
  230. }
  231. catch( Exception e )
  232. {
  233. System.out.println(e.getStackTrace()); // chyba
  234. }
  235. }
  236.  
  237. public void setPass(String pass) {
  238. this.pass = pass;
  239.  
  240. try
  241. {
  242. Statement stmt;
  243. Class.forName("com.mysql.jdbc.Driver");
  244. String url = "jdbc:mysql://localhost:3306/diary";
  245. Connection con = DriverManager.getConnection(url,"root", "");
  246.  
  247. System.out.println("URL: " + url); // debug
  248. System.out.println("Connection: " + con);
  249.  
  250. stmt = con.createStatement();
  251.  
  252. stmt.executeUpdate("UPDATE user SET pass = '"+pass+"' WHERE id='"+this.id+"'");
  253.  
  254. con.close();
  255. }
  256. catch( Exception e )
  257. {
  258. System.out.println(e.getStackTrace()); // chyba
  259. }
  260. }
  261.  
  262. public Boolean addDiaryRecord(String caption, String description, Date started, Date finished)
  263. {
  264. try
  265. {
  266. Statement stmt;
  267. Class.forName("com.mysql.jdbc.Driver");
  268. String url = "jdbc:mysql://localhost:3306/mysql";
  269. Connection con = DriverManager.getConnection(url,"root", "");
  270.  
  271. System.out.println("URL: " + url); // debug
  272. System.out.println("Connection: " + con);
  273.  
  274. stmt = con.createStatement();
  275.  
  276. stmt.executeUpdate("INSERT INTO diaryrecord(caption, description, started, finished, iduser) VALUES('"+caption+"', '"+description+"', "+started+", "+finished+", "+this.id+")");
  277.  
  278. // gets last insertionid
  279. ResultSet rs = stmt.getGeneratedKeys();
  280. int lastInsertedId = rs.getInt(1);
  281.  
  282. DiaryRecord dr = new DiaryRecord(lastInsertedId, caption, description, started, finished, this.id);
  283. this.entries.add(dr);
  284.  
  285. con.close();
  286. return true;
  287. }
  288. catch( Exception e )
  289. {
  290. System.out.println(e.getStackTrace()); // chyba
  291. }
  292. return false;
  293. }
  294.  
  295. // registers new user and returns his instance
  296. public Resolution registerUser()
  297. {
  298. try
  299. {
  300. PreparedStatement pstmt;
  301.  
  302. String url = "jdbc:mysql://localhost:3306/diary";
  303. Class.forName("com.mysql.jdbc.Driver");
  304.  
  305. Connection con = DriverManager.getConnection(url,"root", "");
  306.  
  307. System.out.println("URL: " + url); // debug
  308. System.out.println("Connection: " + con);
  309.  
  310. String sql = "INSERT INTO user(login, pass, mail, name) VALUES('"+login+"', '"+pass+"', '"+mail+"', '"+name+"')";
  311.  
  312. pstmt = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
  313.  
  314. pstmt.executeUpdate();
  315. // gets last insertionid
  316. ResultSet rs = pstmt.getGeneratedKeys();
  317.  
  318. if (rs.next())
  319. this.id = rs.getInt(1);
  320.  
  321. con.close();
  322. }
  323. catch( Exception e )
  324. {
  325. test = e.getMessage();
  326. }
  327. return new ForwardResolution("index.jsp");
  328. }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement