insert操作在我的maven项目中不起作用

balp4ylt  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(489)

这是我的xhtml文件:

  1. <ui:composition
  2. xmlns="http://www.w3.org/1999/xhtml"
  3. xmlns:h="http://java.sun.com/jsf/html"
  4. xmlns:ui="http://java.sun.com/jsf/facelets"
  5. xmlns:p="http://primefaces.org/ui" xmlns:f="http://java.sun.com/jsf/core"
  6. template="../WEB-INF/template/template.xhtml">
  7. <ui:param name="pageTitle" value="Ekleme Sayfası" />
  8. <ui:param name="pageDescription" value="Lütfen aşağıdaki kutucuklara sırayla Kullanıcı Adı, Parola ve E-posta giriniz." />
  9. <ui:define name="content">
  10. <div class="row">
  11. <div class="col-md-12">
  12. <div class="box box-primary">
  13. <h:form id="kullaniciForm">
  14. <!-- <f:facet name="header">-->
  15. <!-- <p:outputPanel>-->
  16. <!-- <h:outputText value="Search all fields:" />-->
  17. <!-- <p:inputText id="globalFilter" onkeyup="propsTable.filter()" style="width:150px" placeholder="Enter keyword"/>-->
  18. <!-- </p:outputPanel>-->
  19. <!-- </f:facet>-->
  20. <div class="box-header with-border">
  21. <h3 class="box-title">Kullanıcı Ekleme Formu : Kullanıcı Adı, Parola, E-posta</h3>
  22. <div class="box-tools">
  23. <p:commandButton action="/kullanici-islemleri/index.xhtml"
  24. value="Geri Dön" styleClass="btn btn-sm btn-primary" icon="fa fa-arrow-left" ajax="false" immediate="true" />
  25. <p:commandButton action="#{userBean.save}"
  26. value="Kaydet" styleClass="btn btn-sm btn-success" icon="fa fa-plus" ajax="false" />
  27. </div>
  28. </div>
  29. <div class="box-body">
  30. <h:panelGrid columns="3">
  31. <p:inputText size="64" id="kullaniciadi" required="true" value="#{userBean.kullanici.username}">
  32. <f:validateLength minimum="4" maximum="64" /></p:inputText>
  33. <p:inputText size="64" id="parola" required="true" value="#{userBean.kullanici.password}">
  34. <f:validateLength minimum="4" maximum="64" /></p:inputText>
  35. <p:inputText size="80" id="eposta" required="true" value="#{userBean.kullanici.email}">
  36. <f:validateLength minimum="4" maximum="255" /></p:inputText>
  37. </h:panelGrid>
  38. </div>
  39. </h:form>
  40. </div>
  41. </div>
  42. </div>
  43. </ui:define>
  44. </ui:composition>

还有kullanici.java、userbean.java和useroperations.java,用于将用户插入postgresql数据库

  1. package com.alper.model.menu;
  2. public class Kullanici {
  3. private String username;
  4. private String password;
  5. private String email;
  6. public Kullanici()
  7. {
  8. this.username = " ";
  9. this.password = " ";
  10. this.email = " ";
  11. }
  12. public Kullanici(String username, String password, String email)
  13. {
  14. this.username = username;
  15. this.password = password;
  16. this.email = email;
  17. }
  18. public String getUsername() {
  19. return username;
  20. }
  21. public String getPassword() {
  22. return password;
  23. }
  24. public String getEmail() {
  25. return email;
  26. }
  27. public void setPassword(String password) {
  28. this.password = password;
  29. }
  30. public void setUsername(String username) {
  31. this.username = username;
  32. }
  33. public void setEmail(String email) {
  34. this.email = email;
  35. }
  36. }
  1. package com.alper.bean.page;
  2. import com.alper.db.UserOperations;
  3. import com.alper.model.menu.Kullanici;
  4. import lombok.Getter;
  5. import lombok.Setter;
  6. import javax.annotation.PostConstruct;
  7. import javax.faces.bean.ManagedBean;
  8. import javax.faces.bean.ViewScoped;
  9. import java.util.*;
  10. @ManagedBean
  11. @ViewScoped
  12. @Getter
  13. @Setter
  14. public class UserBean {
  15. private List<Kullanici> kullanicilar;
  16. private Kullanici kullanici;
  17. private UserOperations userOperations;
  18. @PostConstruct
  19. public void init() {
  20. this.userOperations = new UserOperations();
  21. setKullanicilar(this.userOperations.listUsers());
  22. // fail ise popup yap hatali diye
  23. if(this.kullanicilar == null)
  24. {
  25. System.out.println("DONE");
  26. }else {
  27. System.out.println("FAILED");
  28. }
  29. }
  30. public UserBean() {
  31. this.kullanici = new Kullanici();
  32. }
  33. public void save() {
  34. UserOperations userOperations = new UserOperations();
  35. userOperations.insertUser(getKullanici());
  36. // fail return "/kullanici-islemleri/yeni-kullanici.xhtml" popup yap hatali diye
  37. // success return "/kullanici-islemleri/index.xhtml";
  38. }
  39. public Kullanici getKullanici() {
  40. return kullanici;
  41. }
  42. public void setKullanici(Kullanici kullanici) {
  43. this.kullanici = kullanici;
  44. }
  45. public void setKullanicilar(List<Kullanici> kullanicilar) {
  46. this.kullanicilar = kullanicilar;
  47. }
  48. }
  1. package com.alper.db;
  2. import com.alper.model.menu.Kullanici;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. public class UserOperations extends DBConnection{
  10. private long total_ids = 0;
  11. public List<Kullanici> listUsers() {
  12. List<Kullanici> kullaniciList = new ArrayList<>();
  13. try {
  14. Class.forName("org.postgresql.Driver");
  15. Connection connection = DriverManager.getConnection(getUrl(),getUser(),getPassword());
  16. PreparedStatement preparedStatement = connection.prepareStatement("select * from users");
  17. preparedStatement.execute();
  18. ResultSet resultSet = preparedStatement.getResultSet();
  19. while (resultSet.next()) {
  20. Kullanici kullanici = new Kullanici();
  21. kullanici.setUsername(resultSet.getString(2));
  22. kullanici.setPassword(resultSet.getString(3));
  23. kullanici.setEmail(resultSet.getString(4));
  24. kullaniciList.add(kullanici);
  25. }
  26. connection.close();
  27. } catch (Throwable e) {
  28. e.printStackTrace();
  29. }
  30. return kullaniciList;
  31. }
  32. public void insertUser(Kullanici kullanici) {
  33. String username = kullanici.getUsername();
  34. String password = kullanici.getPassword();
  35. String email = kullanici.getEmail();
  36. try {
  37. Class.forName("org.postgresql.Driver");
  38. Connection connection = DriverManager.getConnection(getUrl(),getUser(),getPassword());
  39. this.total_ids += 1;
  40. // String query = "insert into users (user_id, username, password, email) values (";
  41. // query += this.total_ids + ",'" + username + "','" + password + "','" + email + "');";
  42. // PreparedStatement preparedStatement = connection.prepareStatement(query);
  43. // preparedStatement.executeUpdate();
  44. PreparedStatement preparedStatement = connection.prepareStatement("insert into users values(?,?,?,?)");
  45. preparedStatement.setLong(1,this.total_ids);
  46. preparedStatement.setString(2,username);
  47. preparedStatement.setString(3,password);
  48. preparedStatement.setString(4,email);
  49. preparedStatement.executeUpdate();
  50. System.out.println("Successful");
  51. connection.close();
  52. } catch (Throwable e) {
  53. e.printStackTrace();
  54. System.out.println("Failed");
  55. }
  56. }
  57. }

在xhtml文件中,我使用userbean类将用户信息存储到kullanici.username、kullanici.password等中。通过使用save()方法,我将这些用户名、密码、电子邮件信息传递给useroperations中的insertuser()。我使用的是预先准备好的报表。

hmtdttj4

hmtdttj41#

我修复了它,这是因为我的total_id变量。我增加它的唯一性,但现在我使用随机id生成。现在我可以插入新用户了

相关问题