// End of variables declaration
private java.sql.Connection con;//Declarando as variaveis de Conexão
private java.sql.Statement stmt;//Variavel dos crud
private void IniciarBD() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);// Implementar futurarmente os erros de logo
JOptionPane.showMessageDialog(this, "Erro ao carregar Driver do Banco de Dados"+ e.getMessage());
}
// Estabelecendo Conexão com Banco
String url = "jdbc:sqlserver://localhost;1433/E3Sytems";//+ "databaseName=E3Systems"; // Caminho do Banco
String usuario = " sa "; // Usuário
String senha = " Sqls3rv3r "; // senha
try {
//Classe estabelece a conecxao do driver
con = DriverManager.getConnection(url, usuario, senha);
stmt = con.createStatement();
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Erro na Conexão" + e.getMessage());
}
}
}
// Evento do botão
private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {
String cod = tfCod.getText();
String nome = tfNome.getText();
String sobrenome = tfSobrenome.getText();
String coodcidade = tfCodCidade.getText();
try {
// Insere os dados digitado no banco
stmt.executeUpdate(“INSERT INTO Aluno VALUES( +cod+’, ‘+nome+’,’+sobrenome+’,’+coodcidade+’)”);
try {
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Salvo com Sucesso!!!" + e.getMessage());
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Erro ao tentar salvar" + e.getMessage());
}
}
Segue erro.
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at gui.CadastroAluno.btSalvarActionPerformed(CadastroAluno.java:238)
at gui.CadastroAluno.access$400(CadastroAluno.java:14)
at gui.CadastroAluno$5.actionPerformed(CadastroAluno.java:100)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)