Advertisement
Guest User

Untitled

a guest
Sep 16th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. // registers new user and returns his instance
  2.     public Resolution registerUser()
  3.     {
  4.         try
  5.         {
  6.             PreparedStatement pstmt;
  7.            
  8.             String url = "jdbc:mysql://localhost:3306/diary";
  9.             Class.forName("com.mysql.jdbc.Driver");
  10.            
  11.             Connection con = DriverManager.getConnection(url,"root", "");
  12.            
  13.             System.out.println("URL: " + url); // debug
  14.             System.out.println("Connection: " + con);
  15.              
  16.             String sql = "INSERT INTO user(login, pass, mail, name) VALUES('"+login+"', '"+pass+"', '"+mail+"', '"+name+"')";
  17.            
  18.             pstmt = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
  19.            
  20.             pstmt.executeUpdate();
  21.             // gets last insertionid
  22.             ResultSet rs = pstmt.getGeneratedKeys();
  23.  
  24.             if (rs.next())
  25.                  this.id = rs.getInt(1);
  26.            
  27.             con.close();
  28.          }
  29.          catch( Exception e )
  30.          {
  31.              test = e.getMessage();
  32.          }
  33.         return new ForwardResolution("index.jsp");
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement