staroski 4 de set. de 2018
E onde está o código que deveria persistir a categoria no banco?
MatheusOM 4 de set. de 2018
O model esta assim
package model;
public class Categoria {
private int id ;
private String descricao ;
public Categoria () {
}
public Categoria ( String descricao ) {
this . descricao = descricao ;
}
public int getId () {
return id ;
}
public void setId ( int id ) {
this . id = id ;
}
public String getDescricao () {
return descricao ;
}
public void setDescricao ( String descricao ) {
this . descricao = descricao ;
}
}
E o DAO assim
package dao;
import connection.ConnectionFactory ;
import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.util.ArrayList ;
import java.util.List ;
import model.Categoria ;
public class CategoriaDAO {
private Connection con = null ;
public CategoriaDAO () {
con = ConnectionFactory.getConnection() ;
}
public boolean save ( Categoria categoria ) {
String sql = "INSERT INTO categoria (descricao) VALUES (?)" ;
PreparedStatement stmt = null ;
try {
stmt = con.prepareStatement(sql) ;
stmt.setString(1, categoria.getDescricao()) ;
stmt.executeUpdate() ;
return true ;
} catch ( SQLException ex ) {
System.err.println(" Erro : " + ex );
return false ;
} finally {
ConnectionFactory.closeConnection(con, stmt) ;
}
}
staroski 4 de set. de 2018
E onde você chama o método save da classe CategoriaDAO?
MatheusOM 4 de set. de 2018
Em lugar nenhum. Onde devo chamar?
O projeto esta assim
Pacote connection com o seguinte código.
package connection;
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
public class ConnectionFactory {
private static final String DRIVER = "com.mysql.jdbc.Driver" ;
private static final String URL = "jdbc:mysql://localhost:8889/Balanca" ;
private static final String USER = "root" ;
private static final String PASS = "root" ;
public static Connection getConnection () {
try {
Class . forName ( DRIVER );
return DriverManager . getConnection ( URL , USER , PASS );
} catch ( ClassNotFoundException | SQLException ex ) {
throw new RuntimeException ( "Erro na conexao" , ex );
}
}
public static void closeConnection ( Connection con ) {
if ( con != null ) {
try {
con . close ();
} catch ( SQLException ex ) {
System . out . println ( "Erro: " + ex );
}
}
}
public static void closeConnection ( Connection con , PreparedStatement stmt ) {
if ( stmt != null ) {
try {
stmt . close ();
} catch ( SQLException ex ) {
System . out . println ( "Erro: " + ex );
}
}
closeConnection ( con );
}
public static void closeConnection ( Connection con , PreparedStatement stmt , ResultSet rs ) {
if ( rs != null ) {
try {
rs . close ();
} catch ( SQLException ex ) {
System . out . println ( "Erro: " + ex );
}
}
closeConnection ( con , stmt );
}
}
E no pacote view
package view;
import javafx.application.Application ;
import static javafx . application . Application . launch ;
import javafx.fxml.FXMLLoader ;
import javafx.scene.Parent ;
import javafx.scene.Scene ;
import javafx.stage.Stage ;
/**
*
}
staroski 4 de set. de 2018
Então não vai persistir no banco, pois esse método é responsável por salvar.
No código que é executado quando você deseja salvar.
MatheusOM 4 de set. de 2018
MatheusOM 4 de set. de 2018
Coloquei assim na ação do botão fiz certo?
@FXML
private void botaoEnviar(ActionEvent event) {
Categoria categoria = new Categoria ();
categoria . setDescricao ( txtdescricao . getText ());
System . out . println ( "Voce digitou " + categoria . getDescricao ());
CategoriaDAO categoriadao = new CategoriaDAO ();
categoriadao . save ( categoria );
}
Obs: Desta forma salvo no banco … Mais esta certo?