Olá Pessoal essa classe é para acessar banco de dados mysql, criei um frame, alguns botões para fazer a consulta, compila sem achar erros, mas na execução dá erro acho que é na sintaxe do Mysql.
Obrigado a todos!
Obs. Meu SO é Linux (Ubuntu) e IDE netbeans.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.<em>;
import javax.swing.</em>;
import java.awt.*;
public class MoveRegistros extends JFrame implements ActionListener {
JLabel l1,l2,l3,l4,l5,l6,l7;
JButton b1,b2,b3,b4,b5,b6;
JTextField tfCodigo,tfNome,tfAdmissao,tfDemissao,tfFuncao,tfSexo;
JPanel p1 = new JPanel();
ResultSet rs;
public static void main(String[] args) {
JFrame Janela = new MoveRegistros();
Janela.setVisible(true);
WindowListener x = new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
Janela.addWindowListener(x);
}
public MoveRegistros() {
p1.setLayout(new FlowLayout (FlowLayout.CENTER));
l1 = new JLabel (“Codigo”);
l2 = new JLabel (“Nome”);
l3 = new JLabel (“Admissao”);
l4 = new JLabel (“Demissao”);
l5 = new JLabel (“Funcao”);
l6 = new JLabel (“Sexo”);
l7 = new JLabel ("");
for(int i=0;i<=60;i++)
l7.setText(l7.getText()+"");
tfCodigo = new JTextField(3);
tfCodigo.setEditable(false);
tfNome = new JTextField(50);
tfNome.setEditable(false);
tfAdmissao = new JTextField(10);
tfAdmissao.setEditable(false);
tfDemissao = new JTextField(10);
tfDemissao.setEditable(false);
tfFuncao = new JTextField(50);
tfFuncao.setEditable(false);
tfSexo = new JTextField(1);
tfSexo.setEditable(false);
b1 = new JButton(“Próximo”);
b2 = new JButton(“Anterior”);
b3 = new JButton(“Primeiro”);
b4 = new JButton(“Último”);
b5 = new JButton("+ 10 Registros");
b6 = new JButton("- 10 Registros");
b1.setBackground(new Color(180,180,250));
b2.setBackground(new Color(180,180,250));
b3.setBackground(new Color(180,180,250));
b4.setBackground(new Color(180,180,250));
b5.setBackground(new Color(180,180,250));
b6.setBackground(new Color(180,180,250));
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
p1.add(l1); p1.add(tfCodigo);
p1.add(l2); p1.add(tfNome);
p1.add(l3); p1.add(tfAdmissao);
p1.add(l4); p1.add(tfDemissao);
p1.add(l5); p1.add(tfFuncao);
p1.add(l6); p1.add(tfSexo);
p1.add(l7);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
getContentPane().add(p1);
setTitle(“Tabela de Funcionários da Labordental LTDA”);
setSize(900,200);
setResizable(false);
Connection conn = null;
String teste = “SELECT * FROM FUNCIONARIOS;”;
try {
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection( “jdbc:mysql://localhost/labordental?user=root” );
JOptionPane.showMessageDialog(null,“Estabelecendo Conexão…”,“Conectando”,1);
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(teste);
rs.first();
atualizaCampos();
}
catch(ClassNotFoundException e) {
JOptionPane.showMessageDialog(null,“Excessão Classe não encontrada”,“Conexão Falhou”,2);
e.printStackTrace();
}
catch(SQLException e) {
JOptionPane.showMessageDialog(null,“SQL Exception… Erro na consulta”,“Conexão Falhou”,2);
e.printStackTrace();
}
}
public void actionPerformed (ActionEvent e) {
try{
if (e.getSource()==b1)
rs.next();
if (e.getSource()==b2)
rs.previous();
if (e.getSource()==b3)
rs.first();
if (e.getSource()==b4)
rs.last();
if (e.getSource()==b5)
rs.relative(10);
if (e.getSource()==b6)
rs.relative(-10);
atualizaCampos();
}
catch(SQLException erro) {
JOptionPane.showMessageDialog(null,"Erro no Banco de Dados","Erro",2);
erro.printStackTrace();
}
}
public void atualizaCampos(){
try {
tfCodigo.setText(rs.getString(“CODIGO”));
tfNome.setText(rs.getString(“NOME”));
tfAdmissao.setText(rs.getString(“ADMISSAO”));
tfDemissao.setText(rs.getString(“DEMISSAO”));
tfFuncao.setText(rs.getString(“FUNCAO”));
tfSexo.setText(rs.getString(“SEXO”));
}
catch(SQLException erro) {
JOptionPane.showMessageDialog(null,"Erro no Banco de Dados","Erro",2);
erro.printStackTrace();
}
}
}