Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. Product summary:
  2. - IDE: Eclipse
  3. - Build: Maven
  4. - Framework: Vaadin
  5. - Server: WildFly 10.0.0
  6. - СУБД: PostgreSQL
  7.  
  8. @SuppressWarnings("serial")
  9. public class DatabaseHelper implements Serializable {
  10. private JDBCConnectionPool connectionPool = null;
  11. private static ResourceBundle bundle = ResourceBundle.getBundle("rts.data.databaseHelper");
  12.  
  13. public DatabaseHelper() {
  14. initConnectionPool();
  15. }
  16.  
  17. private void initConnectionPool() {
  18. try {
  19. connectionPool = new SimpleJDBCConnectionPool("org.postgresql.Driver",bundle.getString("connectionUri") ,bundle.getString("userName"), bundle.getString("password"), 2, 2);
  20. } catch (SQLException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. public JDBCConnectionPool getConnectionPool() {
  25. return connectionPool;
  26. }
  27. }
  28.  
  29. connectionUri=jdbc:postgresql://localhost:5432/db.risk
  30. userName=postgres
  31. password=*******
  32.  
  33. loginButton = new Button("Войти",
  34. new Button.ClickListener() {
  35. private static final long serialVersionUID = -5514448801678437295L;
  36.  
  37. @Override
  38. public void buttonClick(ClickEvent event) {
  39. JDBCConnectionPool connectionPool = app.getDbHelper().getConnectionPool();
  40. Connection conn = null;
  41. System.out.println(Crypto.getEncodedStringAlg1(username.getValue(), paswd.getValue()));
  42. try {
  43. conn = connectionPool.reserveConnection();
  44. conn.setAutoCommit(false);
  45. CallableStatement proc = conn.prepareCall(
  46. "SELECT " +
  47. " usr.* " +
  48. "FROM " +
  49. " public.users usr " + //user_s
  50. "WHERE " +
  51. " username = ? and password = ?");
  52. proc.setString(1, username.getValue());
  53. proc.setString(2, Crypto.getEncodedStringAlg1(username.getValue(), paswd.getValue()));
  54. ResultSet rs = proc.executeQuery();
  55. if (rs != null && rs.next()) {
  56. UI.getCurrent().getSession().setAttribute("users_id", rs.getInt("id"));
  57. UI.getCurrent().getSession().setAttribute("users_first_name", rs.getString("first_name"));
  58. UI.getCurrent().getSession().setAttribute("users_surname", rs.getString("surname"));
  59. UI.getCurrent().getSession().setAttribute("users_role", rs.getInt("role"));
  60. if (rs.getInt("role")==2){
  61. app.getNavigator().navigateTo(MyUI.OperView);
  62. }
  63. else
  64. Notification.show("Ошибка:", " Не достаточно прав для входа в систему", Notification.Type.ERROR_MESSAGE);
  65. username.setValue("");
  66. paswd.setValue("");
  67. }
  68. else
  69. Notification.show("Ошибка:", " имя пользователя или пароль не верно", Notification.Type.ERROR_MESSAGE);
  70. conn.close();
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. } finally {
  74. connectionPool.releaseConnection(conn);
  75. }
  76. }
  77. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement