Boa noite galera, to com um problema basico mas nao consigo soluciona-lo…nao consigo inserir no banco…vo postar o codigo abaixo…
Model
package br.com.objetivo.model;
import java.io.Serializable;
public class Serie implements Serializable {
private int idSerie;
private String nomeSerie;
public Serie() {
}
public Serie(int idSerie, String nomeSerie) {
super();
this.idSerie = idSerie;
this.nomeSerie = nomeSerie;
}
public int getIdSerie() {
return idSerie;
}
public void setIdSerie(int idSerie) {
this.idSerie = idSerie;
}
public String getNomeSerie() {
return nomeSerie;
}
public void setNomeSerie(String nomeSerie) {
this.nomeSerie = nomeSerie;
}
}
DAO
package br.com.objetivo.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import br.com.objetivo.jdbc.ConectionFactory;
import br.com.objetivo.model.Serie;
import java.util.ArrayList;
import java.util.List;
public class SerieDAO {
private Connection con;
public SerieDAO() {
this.con = new ConectionFactory().getConnection();
}
public void adiciona(Serie serie) {
System.out.println("cheguei no dao");
String sql = "INSERT INTO SERIE (nomeSerie) values (?)";
try {
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, serie.getNomeSerie());
stmt.execute();
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static SerieDAO getInstance() {
// TODO Auto-generated method stub
return null;
}
}
BEAN
package br.com.objetivo.mb;
import java.io.Serializable;
import br.com.objetivo.dao.SerieDAO;
import br.com.objetivo.model.Serie;
public class SerieMB implements Serializable {
private Serie serie ;
public SerieMB() {
serie = new Serie();
}
public String Salvar() {
try {
SerieDAO.getInstance().adiciona(serie);
} catch (Exception e) {
e.printStackTrace();
}
return "index";
}
public Serie getSerie() {
return serie;
}
public void setSerie(Serie serie) {
this.serie = serie;
}
}
Pagina JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<f:view>
<head>
<title>Formulário Para Cadastro de Serie</title>
</head>
<body>
<h:form>
<h:dataTable value="" border="2" id="s"></h:dataTable>
<fieldset>
<h:panelGrid columns="2" width="100%"
columnClasses="coluna esquerda, coluna direita" rowClasses="normal">
<f:facet name="header">
<h:outputLabel value="Cadastro de aluno"></h:outputLabel>
</f:facet>
<h:outputLabel value="Serie" for="serie" />
<h:inputText value="#{serieMB.serie.nomeSerie}" size="30" />
<h:commandButton value="Salvar" action="#{serieMB.Salvar}" />
</h:panelGrid>
</fieldset>
</h:form>
</body>
</f:view>
Faces Config
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<managed-bean-name>serieMB</managed-bean-name>
<managed-bean-class>br.com.objetivo.mb.SerieMB</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>index</display-name>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
Erro
java.lang.NullPointerException
at br.com.objetivo.mb.SerieMB.Salvar(SerieMB.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:70)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
web xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/webapp_2_5.xsd">
<context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
