Advertisement
Guest User

Untitled

a guest
Mar 15th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.94 KB | None | 0 0
  1. public class CreaQuestionario {
  2.  
  3.     private UserSession user;
  4.     private String titolo;
  5.     private String categoria;
  6.     private static final String admin = "fabio@marcoccia.net";
  7.  
  8.     private UserSession getUserInSession() {
  9.         HttpSession session = ServletActionContext.getRequest().getSession();
  10.         return (UserSession) session.getAttribute(UserSession.KEY);
  11.     }
  12.  
  13.     public String execute() {
  14.         user = getUserInSession();
  15.         Statement stmt = null;
  16.         Connection conn = null;
  17.         if (user == null) {
  18.             return "erroreNonLoggato";
  19.         } else {
  20.             try {
  21.                 String URL = "jdbc:mysql://localhost:3308/tutorial";
  22.                 Class.forName("com.mysql.jdbc.Driver");
  23.                 conn = DriverManager.getConnection(URL, "root", "Fabio1992");
  24.                 stmt = conn.createStatement();
  25.                 String sql = "INSERT INTO tutorial.questionario values(0,"
  26.                         + "'" + getTitolo() +"', '" + getCategoria() + "', '" + admin + "')";
  27.                 System.out.println(sql);
  28.                 stmt.executeUpdate(sql);
  29.             } catch (Exception e) {
  30.                 e.printStackTrace();
  31.             } finally {
  32.                 try {
  33.                     if (stmt != null)
  34.                         conn.close();
  35.                 } catch (SQLException se) {
  36.                 }
  37.                 if (conn != null) {
  38.                     try {
  39.                         conn.close();
  40.                     } catch (Exception e) {
  41.                     }
  42.                 }
  43.             }
  44.             return "pagina_admin";
  45.         }
  46.     }
  47.  
  48.     public String getCategoria() {
  49.         return categoria;
  50.     }
  51.  
  52.     public void setCategoria(String categoria) {
  53.         this.categoria = categoria;
  54.     }
  55.  
  56.     public String getTitolo() {
  57.         return titolo;
  58.     }
  59.  
  60.     public void setTitolo(String titolo) {
  61.         this.titolo = titolo;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement