Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package web;
- import diaryclasses.DiaryRecord;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import net.sourceforge.stripes.action.ActionBean;
- import net.sourceforge.stripes.action.ActionBeanContext;
- import net.sourceforge.stripes.action.DefaultHandler;
- import net.sourceforge.stripes.action.ForwardResolution;
- import net.sourceforge.stripes.action.Resolution;
- import net.sourceforge.stripes.validation.Validate;
- import org.stripesstuff.plugin.session.Session;
- /**
- *
- * @author Satik
- */
- public class UserActionBean implements ActionBean{
- ActionBeanContext context;
- @Override
- public void setContext(ActionBeanContext context) {
- this.context = context;
- }
- @Override
- public ActionBeanContext getContext() {
- return this.context;
- }
- @Session
- @Validate(required=true, minlength=3, maxlength=50)
- private String login;
- @Session
- @Validate(required=true, minlength=3, maxlength=50)
- private String pass;
- @Session
- @Validate(required=true, minlength=3, maxlength=100)
- private String mail;
- @Session
- @Validate(required=true, minlength=3, maxlength=100)
- private String name;
- @Session
- private int id;
- @Session
- private String test;
- public void setTest(String test) {
- this.test = test;
- }
- public String getTest() {
- return test;
- }
- @Session
- private List<DiaryRecord> entries = new ArrayList<DiaryRecord>();
- @DefaultHandler
- public Resolution Login()
- {
- name = login;
- if (LoadFromDb(login, pass))
- {
- int count = LoadDiary(); // do count vrati pocet zaznamu v diari
- // pokud se povede login - resolution
- }
- else
- {
- // pokud se login nepovede - resolution
- }
- return new ForwardResolution("index.jsp");
- }
- private Boolean LoadFromDb(String login, String password)
- {
- Boolean loginFound = false;
- try
- {
- Statement stmt;
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/diary";
- Connection con = DriverManager.getConnection(url,"root", "");
- System.out.println("URL: " + url); // debug
- System.out.println("Connection: " + con);
- stmt = con.createStatement();
- //stmt.executeUpdate("CREATE DATABASE JunkDB");
- ResultSet res = stmt.executeQuery("SELECT * FROM user WHERE login='"+login+"' AND pass='"+password+"'");
- while (res.next())
- {
- this.id = res.getInt("id");
- this.login = res.getString("login");
- this.pass = res.getString("pass");
- this.mail = res.getString("mail");
- this.name = res.getString("name");
- loginFound = true;
- }
- con.close();
- }
- catch( Exception e )
- {
- System.out.println(e.getStackTrace()); // chyba
- return false;
- }
- return loginFound;
- }
- private int LoadDiary()
- {
- int count = 0;
- try
- {
- Statement stmt;
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/diary";
- Connection con = DriverManager.getConnection(url,"root", "");
- System.out.println("URL: " + url); // debug
- System.out.println("Connection: " + con);
- stmt = con.createStatement();
- //stmt.executeUpdate("CREATE DATABASE JunkDB");
- ResultSet res = stmt.executeQuery("SELECT * FROM diaryrecord WHERE iduser='"+this.id+"'");
- while (res.next())
- {
- DiaryRecord dr = new DiaryRecord(res.getInt("id"), res.getString("caption"), res.getString("description"), res.getDate("started"), res.getDate("finished"), res.getInt("iduser"));
- this.entries.add(dr);
- count ++;
- }
- con.close();
- }
- catch( Exception e )
- {
- System.out.println(e.getStackTrace()); // chyba
- }
- return count;
- }
- /*public UserActionBean(int id, String login, String pass, String mail, String name) {
- this.id = id;
- this.login = login;
- this.pass = pass;
- this.mail = mail;
- this.name = name;
- }*/
- public int getId() {
- return id;
- }
- public void setEntries(List<DiaryRecord> entries) {
- this.entries = entries;
- }
- public void setId(int id) {
- this.id = id;
- }
- public void setLogin(String login) {
- this.login = login;
- }
- public void setName(String name) {
- this.name = name;
- }
- public List<DiaryRecord> getEntries() {
- return entries;
- }
- public String getLogin() {
- return login;
- }
- public String getMail() {
- return mail;
- }
- public String getName() {
- return name;
- }
- public String getPass() {
- return pass;
- }
- public void setMail(String mail) {
- this.mail = mail;
- try
- {
- Statement stmt;
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/diary";
- Connection con = DriverManager.getConnection(url,"root", "");
- System.out.println("URL: " + url); // debug
- System.out.println("Connection: " + con);
- stmt = con.createStatement();
- stmt.executeUpdate("UPDATE user SET mail = '"+mail+"' WHERE id='"+this.id+"'");
- con.close();
- }
- catch( Exception e )
- {
- System.out.println(e.getStackTrace()); // chyba
- }
- }
- public void setPass(String pass) {
- this.pass = pass;
- try
- {
- Statement stmt;
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/diary";
- Connection con = DriverManager.getConnection(url,"root", "");
- System.out.println("URL: " + url); // debug
- System.out.println("Connection: " + con);
- stmt = con.createStatement();
- stmt.executeUpdate("UPDATE user SET pass = '"+pass+"' WHERE id='"+this.id+"'");
- con.close();
- }
- catch( Exception e )
- {
- System.out.println(e.getStackTrace()); // chyba
- }
- }
- public Boolean addDiaryRecord(String caption, String description, Date started, Date finished)
- {
- try
- {
- Statement stmt;
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/mysql";
- Connection con = DriverManager.getConnection(url,"root", "");
- System.out.println("URL: " + url); // debug
- System.out.println("Connection: " + con);
- stmt = con.createStatement();
- stmt.executeUpdate("INSERT INTO diaryrecord(caption, description, started, finished, iduser) VALUES('"+caption+"', '"+description+"', "+started+", "+finished+", "+this.id+")");
- // gets last insertionid
- ResultSet rs = stmt.getGeneratedKeys();
- int lastInsertedId = rs.getInt(1);
- DiaryRecord dr = new DiaryRecord(lastInsertedId, caption, description, started, finished, this.id);
- this.entries.add(dr);
- con.close();
- return true;
- }
- catch( Exception e )
- {
- System.out.println(e.getStackTrace()); // chyba
- }
- return false;
- }
- // registers new user and returns his instance
- public Resolution registerUser()
- {
- try
- {
- PreparedStatement pstmt;
- String url = "jdbc:mysql://localhost:3306/diary";
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection(url,"root", "");
- System.out.println("URL: " + url); // debug
- System.out.println("Connection: " + con);
- String sql = "INSERT INTO user(login, pass, mail, name) VALUES('"+login+"', '"+pass+"', '"+mail+"', '"+name+"')";
- pstmt = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
- pstmt.executeUpdate();
- // gets last insertionid
- ResultSet rs = pstmt.getGeneratedKeys();
- if (rs.next())
- this.id = rs.getInt(1);
- con.close();
- }
- catch( Exception e )
- {
- test = e.getMessage();
- }
- return new ForwardResolution("index.jsp");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement