【发布时间】:2016-10-22 21:52:42
【问题描述】:
我在调用基本插入方法时遇到了一些问题。当它进入方法时,它会在 try and catch 中死掉,而不会向我的数据库中插入任何东西。还注意到属性 required="true" 似乎不起作用。
这是我当前的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<ui:include src="common/timeout.xhtml" />
<h:head>
<meta charset="utf-8"></meta>
<meta http-equiv="X-UA-Compatible" content="IE=edge"></meta>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"></meta>
</h:head>
<h:body >
<!-- Botonera -->
<div id="botones">
<p:commandButton id="btnIn" action="#{prueba.guardar}" update="growl" icon="fa fa-floppy-o text-info" />
</div>
<!-- Ajax loader -->
<div class="pull-right" style="margin-top: -40px; margin-right:5px;">
<ui:include src="common/loader.xhtml"/>
</div>
<!-- Mensaje -->
<p:messages globalOnly="true" id="growl" closable="true" showIcon="true" style="margin-left:5px; margin-right:5px;"/>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-4">
<p:inputText id="codigo" maxlength="50" required="true" requiredMessage="#{bd.getMessage('htmlvalidate')}" value="#{prueba.codigo}"/>
<p:watermark for="codigo" value="#{msg.sgc001cod}" />
<p:spacer height="1"/>
<p:message for="codigo"/>
<p:inputText id="desc" maxlength="100" required="true" requiredMessage="#{bd.getMessage('htmlvalidate')}" value="#{prueba.desc}"/>
<p:watermark for="desc" value="#{msg.sgc001des}" />
<p:spacer height="1"/>
<p:message for="desc"/>
</div>
</div>
<h:inputHidden id="vop" value="#{prueba.validarOperacion}"/>
</h:body>
</html>
和我的 BEAN(恢复):
package org.enlacerh.util;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.enlacerh.util.PntGenerica;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.primefaces.model.LazyDataModel;
/**
*
* @author Mauricio
*/
@ManagedBean
@ViewScoped
public class Prueba extends Bd implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private LazyDataModel<Prueba> lazyModel;
/**
* @return the lazyModel
*/
public LazyDataModel<Prueba> getLazyModel() {
return lazyModel;
}
@PostConstruct
public void init() {
System.out.println("entre al metodo INIT");
System.out.println("Usuario: " + login.toUpperCase());
lazyModel = new LazyDataModel<Prueba>(){
/**
*
*/
private static final long serialVersionUID = 7217573531435419432L;
};
}
private String codigo = "";
private String desc = "";
private int validarOperacion = 0;
private String zcodigo = "";
private String zdesc = "";
public String getCodigo() {
return codigo;
}
public void setCodigo(String codigo) {
this.codigo = codigo;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getZcodigo() {
return zcodigo;
}
public void setZcodigo(String zcodigo) {
this.zcodigo = zcodigo;
}
public String getZdesc() {
return zdesc;
}
public void setZdesc(String zdesc) {
this.zdesc = zdesc;
}
/**
* @return the validarOperacion
*/
public int getValidarOperacion() {
return validarOperacion;
}
/**
* @param validarOperacion the validarOperacion to set
*/
public void setValidarOperacion(int validarOperacion) {
this.validarOperacion = validarOperacion;
}
//Formateador de la fecha sdfecha
//java.text.SimpleDateFormat sdfecha = new java.text.SimpleDateFormat("dd/MM/yyyy", locale);
//String fecha = sdfecha.format(fecact); //Fecha formateada para insertar en tablas
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Variables seran utilizadas para capturar mensajes de errores de Oracle y parametros de metodos
FacesMessage msj = null;
PntGenerica consulta1 = new PntGenerica();
boolean vGacc; //Validador de opciones del menú
//private int rows1; //Registros de tabla oracle
private String login = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("usuario"); //Usuario logeado
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Coneccion a base de datos
//Pool de conecciones JNDI
Connection con;
PreparedStatement pstmt = null;
ResultSet r;
/**
* Inserta categoria1.
* <p>
* <b>Parametros del Metodo:<b> String codcat1, String descat1 unidos como un solo string.<br>
* String pool, String login.<br><br>
**/
public void insert() throws NamingException {
System.out.println("entre al metodo INSERT");
try {
Context initContext = new InitialContext();
DataSource ds = (DataSource) initContext.lookup(JNDI);
con = ds.getConnection();
String query = "INSERT INTO PRUEBA1 VALUES (?,?,?,'" + getFecha() + "',?,'" + getFecha() + "')";
pstmt = con.prepareStatement(query);
pstmt.setString(1, codigo.toUpperCase());
pstmt.setString(2, desc.toUpperCase());
pstmt.setString(3, login);
pstmt.setString(4, login);
System.out.println(query);
System.out.println("codigo: " + codigo);
System.out.println("desc: " + desc);
try {
System.out.println("entre al try");
//Avisando
pstmt.executeUpdate();
msj = new FacesMessage(FacesMessage.SEVERITY_INFO, getMessage("msnInsert"), "");
limpiarValores();
} catch (SQLException e) {
System.out.println("entre al catch");
msj = new FacesMessage(FacesMessage.SEVERITY_FATAL, e.getMessage(), "");
}
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
FacesContext.getCurrentInstance().addMessage(null, msj);
}
public void guardar() throws NamingException, SQLException{
if(validarOperacion==0){
System.out.println("entre al metodo guardar");
insert();
} else {
System.out.println("entre al metodo update");
//update();
}
}
private void limpiarValores() {
// TODO Auto-generated method stub
codigo = "";
desc = "";
validarOperacion = 0;
}
}
这是我从 System.out.println 看到的消息:
entre al metodo guardar
entre al metodo INSERT INTO PRUEBA1 VALUES (?,?,?,'22/oct/2016',?,'22/oct/2016',?)
codigo:
desc:
entre al try
entre al catch
当我点击“GUARDAR”按钮时,我收到:
ORA-01400: 无法将 NULL 插入 ("OPENBIZVIEW"."PRUEBA1"."CODIGO")
显然它没有正确接收值,因此即使我在两个文本输入中都插入了值,它也会变为 null。
我也在控制台上看到这条消息:
2016 年 10 月 25 日下午 3:35:42 com.sun.faces.context.ExternalContextImpl getMimeType ADVERTENCIA: JSF1091: no se ha encontrado ningún Tipo MIME para el archivo fa/fontawesome-webfont.woff2。 Para resolverlo, agregue una asignación de tipo MIME al archivo web.xml de la aplicación。
【问题讨论】:
-
您没有在插入语句中列出列名,这是不好的做法,因为很容易以错误的顺序获取值 - 我们无法判断
codigo应该是哪个值是。表定义是什么?哪些列被定义为非空?您是否调试过实际设置的值 - 您确定 inputText(s) 中的值为 null,而不是login? (为什么不将日期列设置为绑定变量??) -
请看:How to create a Minimal, Complete, and Verifiable example,特别是Minimal副标题。
-
感谢您的评论,请尝试将代码设置为最低要求,看看我是否有同样的错误。如果是这样。生病更新我的帖子。再次感谢。
-
所以我按照您的建议应用并使其成为最低价值产品,实际上是 2 个带有命令按钮的输入,其他所有内容都已删除,但仍然出现相同的错误。请帮忙!