-1
I’m having a problem loading the data into a Jtable, use BD Derby, the database connection works more when trying to open the Jframe Cad_usuarios appears the error below:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Controle.ConexaoBD.executaSQL(ConexaoBD.java:57) at Visao.Cad_Usuarios.preencherTabela(Cad_Usuarios.java:235) at Visao.Cad_Usuarios.<init>(Cad_Usuarios.java:32) at Visao.cadastro.jMUsuariosActionPerformed(cadastro.java:147) at Visao.cadastro.access$300(cadastro.java:9) at Visao.cadastro$4.actionPerformed(cadastro.java:96) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.AbstractButton.doClick(AbstractButton.java:376) at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833) at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877) at java.awt.Component.processMouseEvent(Component.java:6533) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
User class:
package Modelo;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="ROOT.USUARIO")
public class Usuario implements Serializable{
@Id
@Column(name = "LOGIN", nullable = false)
private String login;
@Column(name = "SENHA", nullable = false)
private String senha;
@Column(name = "NOME", nullable = false)
private String nome;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Usuario(){}
}
Form Cad_usuarios
package Visao;
import Controle.ConexaoBD;
import Modelo.ModeloTabela;
import Modelo.Usuario;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import static org.eclipse.persistence.jpa.jpql.parser.Expression.SQL;
public class Cad_Usuarios extends javax.swing.JFrame {
EntityManagerFactory factory;
EntityManager manager;
ConexaoBD conecta = new ConexaoBD();
public Cad_Usuarios() {
initComponents();
preencherTabela("select * from usuarios order by login");
try{
factory = Persistence.createEntityManagerFactory("Ponto_DablioPU");
manager = factory.createEntityManager();
}catch(Exception e){
System.exit(1);
}
}
public void limpar(){
jTLogin.setText(null);
jTNome.setText(null);
jPFSenha.setText(null);
}
private void jBSalvarActionPerformed(java.awt.event.ActionEvent evt) {
Usuario user = new Usuario();
user.setLogin(jTLogin.getText());
user.setNome(jTNome.getText());
user.setSenha(jPFSenha.getText());
manager.getTransaction().begin();
manager.persist(user);
manager.getTransaction().commit();
JOptionPane.showMessageDialog(null, "Usuário Cadastrado com Sucesso!");
limpar();
}
private void jBSairActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
private void jBLimparActionPerformed(java.awt.event.ActionEvent evt) {
limpar();
}
public void preencherTabela(String SQL){
ArrayList dados = new ArrayList();
String[] Colunas = new String[]{"Login","Senha","Nome"};
conecta.executaSQL(SQL);
try{
conecta.rs.first();
do{
dados.add(new Object[]{conecta.rs.getInt("login"),conecta.rs.getString("senha"),conecta.rs.getString("nome")});
}while(conecta.rs.next());
}catch (SQLException ex){
JOptionPane.showMessageDialog(null,"Erro ao preencher o ArrayList! \n ERRO:");
}
ModeloTabela modelo = new ModeloTabela(dados, Colunas);
jTable1.setModel(modelo);
jTable1.getColumnModel().getColumn(0).setPreferredWidth(150);
jTable1.getColumnModel().getColumn(0).setResizable(false);
jTable1.getColumnModel().getColumn(1).setPreferredWidth(150);
jTable1.getColumnModel().getColumn(1).setResizable(false);
jTable1.getColumnModel().getColumn(2).setPreferredWidth(80);
jTable1.getColumnModel().getColumn(2).setResizable(false);
jTable1.getTableHeader().setReorderingAllowed(false);
jTable1.setAutoResizeMode(jTable1.AUTO_RESIZE_OFF);
jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
Modelotabela class
package Modelo;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class ModeloTabela extends AbstractTableModel{
private ArrayList linhas = null;
private String[] colunas = null;
public ModeloTabela(ArrayList lin,String[] col){
setLinhas(lin);
setColunas(col);
}
public ArrayList getLinhas(){
return linhas;
}
public void setLinhas(ArrayList dados){
linhas = dados;
}
public String [] getColunas(){
return colunas;
}
public void setColunas(String[] nomes){
colunas = nomes;
}
public int getColumnCount(){
return colunas.length;
}
public int getRowCount(){
return linhas.size();
}
public String getColumnName(int numCol){
return colunas[numCol];
}
public Object getValueAt(int numLin, int numCol){
Object[] linha = (Object[])getLinhas().get(numLin);
return linha[numCol];
}
}
Conexaobd Class
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class ConexaoBD {
public Statement stm;
public ResultSet rs;
private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private String caminho = "jdbc:derby://localhost:1527/ponto_dablio";
private String usuario = "root";
private String senha = "root";
public Connection connection;
public void conexao(){
try {
System.setProperty("jdbc.Drivers", driver);
connection = DriverManager.getConnection(caminho, usuario, senha);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,"CONECTADO COM SUCESSO!!\n Erro:" + ex.getMessage());
}
}
public void executaSQL(String sql) {
try {
stm = connection.createStatement(rs.TYPE_SCROLL_INSENSITIVE,rs.CONCUR_READ_ONLY);
rs = stm.executeQuery(sql);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,"ERRO DE EXECUTASQL!!\n ERRO: " + ex.getMessage());
}
}
public void desconecta(){
try{
connection.close();
}catch (SQLException ex){
JOptionPane.showMessageDialog(null,"Erro ao fechar conexão!\n Erro:" + ex.getMessage());
}
}
}
I made the change but keeps giving error says that the table "users" does not exist, error:
`Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Visao.Cad_Usuarios.preencherTabela(Cad_Usuarios.java:238) at Visao.Cad_Usuarios.<init>(Cad_Usuarios.java:32) at Visao.cadastro.jMUsuariosActionPerformed(cadastro.java:147) at Visao.cadastro.access$300(cadastro.java:9) at Visao.cadastro$4.actionPerformed(cadastro.java:96) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.AbstractButton.doClick(AbstractButton.java:376) at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833) at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877) at java.awt.Component.processMouseEvent(Component.java:6533) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
I made the change further continued giving error, I wrote the error below, thank you
– Dhouglas Silva Gomes
sorry, I’m new, I added above the error presented after making the informed change.
– Dhouglas Silva Gomes