Veja uma nova transformação da classe de incluir dados, onde oferecemos um método para incluir dados que neste exemplo é usada por uma interface com o usuário.
package br.com.thz.banco;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class IncluirBanco3 {
public static void incluir(String nome) throws Exception {
ResultSet resultado = null;
Statement instrucao = null;
try {
String sql = “INSERT INTO cliente ( nome ) VALUES (‘”
+ nome + “‘)”;
instrucao = Conexao.getConnection().createStatement();
instrucao.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
throw new Exception(“Erro inserindo dados de cliente”);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
if (resultado != null) {
resultado.close();
}
if (instrucao != null) {
instrucao.close();
}
}
}

package br.com.thz.banco;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class IncluirBanco3Interface extends JFrame implements ActionListener {
JButton b1;
JTextField textField;
JTextArea textArea;
JLabel pizza;
public IncluirBanco3Interface() {
b1 = new JButton(“Incluir”);
b1.setMnemonic(KeyEvent.VK_I);
b1.setActionCommand(“incluir”);
b1.addActionListener(this);
b1.setToolTipText(“Click neste botão para incluir”);
ImageIcon icone = new ImageIcon(“image/user.gif”);
pizza = new JLabel(“inclusão de cliente”, icone, JLabel.LEFT);
textField = new JTextField(15);
textField.addActionListener(this);
textArea = new JTextArea(10, 15);
JScrollPane scroll = new JScrollPane(textArea);
setIconImage(icone.getImage());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
Container c = getContentPane();
JPanel painel = new JPanel();
painel.setLayout(new GridLayout(3,1));
painel.add(pizza);
painel.add(textField);
painel.add(b1);
c.setLayout(new BorderLayout());
c.add(painel, BorderLayout.NORTH);
c.add(scroll, BorderLayout.CENTER);
pack();
}
public void actionPerformed(ActionEvent event) {
String nome = textField.getText();
try {
IncluirBanco3.incluir(nome);
textArea.append(nome + ” incluido “);
} catch (Exception e) {
textArea.append(“erro ao incluir ” + nome + ” ” +
e.getMessage());
}
textField.selectAll();
}
public static void main(String[] args) {
IncluirBanco3Interface janela = new IncluirBanco3Interface();
janela.show();
}
}