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

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

这是我的xhtml文件:

<ui:composition
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:p="http://primefaces.org/ui" xmlns:f="http://java.sun.com/jsf/core"
        template="../WEB-INF/template/template.xhtml">

  <ui:param name="pageTitle" value="Ekleme Sayfası" />
  <ui:param name="pageDescription" value="Lütfen aşağıdaki kutucuklara sırayla Kullanıcı Adı, Parola ve E-posta giriniz." />

  <ui:define name="content">
    <div class="row">
      <div class="col-md-12">
        <div class="box box-primary">

          <h:form id="kullaniciForm">
            <!--                        <f:facet name="header">-->
            <!--                            <p:outputPanel>-->
            <!--                                <h:outputText value="Search all fields:" />-->
            <!--                                <p:inputText id="globalFilter" onkeyup="propsTable.filter()" style="width:150px" placeholder="Enter keyword"/>-->
            <!--                            </p:outputPanel>-->
            <!--                        </f:facet>-->

            <div class="box-header with-border">
              <h3 class="box-title">Kullanıcı Ekleme Formu : Kullanıcı Adı, Parola, E-posta</h3>
              <div class="box-tools">
                <p:commandButton action="/kullanici-islemleri/index.xhtml"
                                 value="Geri Dön" styleClass="btn btn-sm btn-primary" icon="fa fa-arrow-left" ajax="false" immediate="true" />
                <p:commandButton action="#{userBean.save}"
                                 value="Kaydet" styleClass="btn btn-sm btn-success" icon="fa fa-plus" ajax="false" />
              </div>
            </div>

            <div class="box-body">

                <h:panelGrid columns="3">

                  <p:inputText size="64" id="kullaniciadi" required="true" value="#{userBean.kullanici.username}">
                    <f:validateLength minimum="4" maximum="64" /></p:inputText>

                  <p:inputText size="64" id="parola" required="true" value="#{userBean.kullanici.password}">
                    <f:validateLength minimum="4" maximum="64" /></p:inputText>

                  <p:inputText size="80" id="eposta" required="true" value="#{userBean.kullanici.email}">
                    <f:validateLength minimum="4" maximum="255" /></p:inputText>

                </h:panelGrid>

            </div>

          </h:form>

        </div>
      </div>
    </div>

  </ui:define>

</ui:composition>

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

package com.alper.model.menu;

public class Kullanici {
    private String username;
    private String password;
    private String email;

    public Kullanici()
    {
        this.username = " ";
        this.password = " ";
        this.email = " ";
    }

    public Kullanici(String username, String password, String email)
    {
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getEmail() {
        return email;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}
package com.alper.bean.page;

import com.alper.db.UserOperations;
import com.alper.model.menu.Kullanici;
import lombok.Getter;
import lombok.Setter;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.util.*;

@ManagedBean
@ViewScoped
@Getter
@Setter
public class UserBean {

    private List<Kullanici> kullanicilar;
    private Kullanici kullanici;
    private UserOperations userOperations;

    @PostConstruct
    public void init() {
        this.userOperations = new UserOperations();
        setKullanicilar(this.userOperations.listUsers());
        // fail ise popup yap hatali diye
        if(this.kullanicilar == null)
        {
            System.out.println("DONE");
        }else {
            System.out.println("FAILED");
        }

    }

    public UserBean() {
        this.kullanici = new Kullanici();
    }

    public void save() {
        UserOperations userOperations = new UserOperations();
        userOperations.insertUser(getKullanici());
        // fail return "/kullanici-islemleri/yeni-kullanici.xhtml" popup yap hatali diye
        // success return "/kullanici-islemleri/index.xhtml";
    }

    public Kullanici getKullanici() {
        return kullanici;
    }

    public void setKullanici(Kullanici kullanici) {
        this.kullanici = kullanici;
    }

    public void setKullanicilar(List<Kullanici> kullanicilar) {
        this.kullanicilar = kullanicilar;
    }
}
package com.alper.db;

import com.alper.model.menu.Kullanici;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserOperations extends DBConnection{

    private long total_ids = 0;

    public List<Kullanici> listUsers() {
        List<Kullanici> kullaniciList = new ArrayList<>();

        try {
            Class.forName("org.postgresql.Driver");
            Connection connection = DriverManager.getConnection(getUrl(),getUser(),getPassword());
            PreparedStatement preparedStatement = connection.prepareStatement("select * from users");
            preparedStatement.execute();
            ResultSet resultSet = preparedStatement.getResultSet();

            while (resultSet.next()) {
                Kullanici kullanici = new Kullanici();
                kullanici.setUsername(resultSet.getString(2));
                kullanici.setPassword(resultSet.getString(3));
                kullanici.setEmail(resultSet.getString(4));
                kullaniciList.add(kullanici);
            }
            connection.close();

        } catch (Throwable e) {
            e.printStackTrace();
        }
        return kullaniciList;
    }

    public void insertUser(Kullanici kullanici) {
        String username = kullanici.getUsername();
        String password = kullanici.getPassword();
        String email = kullanici.getEmail();
        try {
            Class.forName("org.postgresql.Driver");
            Connection connection = DriverManager.getConnection(getUrl(),getUser(),getPassword());
            this.total_ids += 1;
//            String query = "insert into users (user_id, username, password, email) values (";
//            query += this.total_ids + ",'" + username + "','" + password + "','" + email + "');";
//            PreparedStatement preparedStatement = connection.prepareStatement(query);
//            preparedStatement.executeUpdate();
            PreparedStatement preparedStatement = connection.prepareStatement("insert into users values(?,?,?,?)");
            preparedStatement.setLong(1,this.total_ids);
            preparedStatement.setString(2,username);
            preparedStatement.setString(3,password);
            preparedStatement.setString(4,email);
            preparedStatement.executeUpdate();
            System.out.println("Successful");
            connection.close();

        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println("Failed");
        }
    }
}

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

hmtdttj4

hmtdttj41#

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

相关问题