Estou mandando o códgio completo entao!
package view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import model.BancoDados;
public class Painel extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
private JButton pesquisar = null;
private JTextField jtPalavra = null;
private JLabel palavra = null;
private JTextPane area = null;
private JScrollPane barra = null;
private JComboBox combo = null;
private BancoDados banco = null;
public String acesso = null; //AQUI A VARIAVEL QUE DEVE CONTER O ARGUMENTO AO BD
public Painel(){
super(new BorderLayout());
setLayout(null);
add(getCombo());
add(getLabelPalavra());
add(getTextPalavra());
add(getBotaoPesquisar());
add(getBarra());
setBackground(Color.DARK_GRAY);
add(getImg());
}
public JComboBox getCombo(){ //lista
if (combo == null){
combo = new JComboBox();
combo.addItem("LOVE");
combo.addItem("NEED");
combo.addItem("WISH");
combo.setBounds(5, 180, 110, 30);
Box box = Box.createVerticalBox();
box.setBorder(BorderFactory.createTitledBorder(" PALAVRA: "));
box.add(combo);
combo.addActionListener(this);
}
return combo;
}
public JTextPane getArea(){ //area do texto de saida
if (area == null){
area = new JTextPane();
area.setFont(new Font("Times", Font.BOLD, 14)); //fonte do texto
area.setBounds(120, 130, 500, 300);
area.setEditable(false); //impedir a edicao
}
return area;
}
public JScrollPane getBarra(){ //barra de rolagem
if (barra == null){
barra = new JScrollPane(getArea());
barra.setBounds(120, 130, 500, 300);
barra.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); //default
barra.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); //default
}
return barra;
}
public JLabel getImg() { //IMAGEM do painel
JLabel img = new JLabel();
img.setIcon(new javax.swing.ImageIcon("c:/logotipo.jpg"));
img.setBounds(3, 3, 252, 83);
return img;
}
public JButton getBotaoPesquisar(){ // botao pesquisar
if(pesquisar == null){
pesquisar = new JButton("Pesquisar");
pesquisar.setBounds(3, 130, 100, 22);
pesquisar.addActionListener(this);
}
return pesquisar;
}
public JLabel getLabelPalavra(){ //inserir rotulo palavra
if(palavra == null){
palavra = new JLabel("Palavra:");
palavra.setBounds(4, 100, 100, 22);
palavra.setFont(new Font("Courier new", Font.BOLD, 20));
palavra.setForeground(Color.ORANGE); //cor da fonte
}
return palavra;
}
public JTextField getTextPalavra(){ // caixa de texto de palavra
if(jtPalavra == null){
jtPalavra = new JTextField(40);
jtPalavra.setBounds(120, 100, 200, 22);
jtPalavra.addActionListener(this);
}
return jtPalavra;
}
public void actionPerformed(ActionEvent evt){
Object fonte = evt.getSource();
if (fonte == getBotaoPesquisar()){
try {
acesso = getTextPalavra().getText(); //PEGANDO O TEXTO DIGITADO
banco = new BancoDados(); //CHAMANDO O CONSTRUTOR DO BD
getArea().setText(banco.nome.toUpperCase()+"\n"+banco.texto);
getTextPalavra().selectAll();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.toString() + " " + e.getMessage(), "Erro",
JOptionPane.ERROR_MESSAGE);
}
}
JComboBox cb = (JComboBox)evt.getSource();
String name = (String)cb.getSelectedItem();
atualizaLabel(name);
}
public void atualizaLabel(String name){
if(name == "LOVE"){
File file = new File("c:/dic/love.txt");
try {
FileReader fr= new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s = "";
String d = "";
while ((s = br.readLine()) != null){
d += s + "\n";
}
getArea().setText(d);
} catch (FileNotFoundException fnfe) {
JOptionPane.showMessageDialog(null, "Arquivo não encontrado.");
fnfe.printStackTrace();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
if(name == "NEED"){
File file = new File("c:/dic/need.txt");
try{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s = "";
String d = "";
while ((s = br.readLine()) != null){
d += s + "\n";
}
getArea().setText(d);
}catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}catch (IOException ioe) {
ioe.printStackTrace();
}
}
if(name == "WISH"){
File file = new File("c:/dic/wish.txt");
try{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s = "";
String d = "";
while ((s = br.readLine()) != null){
d += s + "\n";
}
getArea().setText(d);
}catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public Object getDado(Object dados) throws Exception{
Dominio dominio = (Dominio) dados;
dominio.setPalavra(getTextPalavra().getText());
return dominio;
}
}
A seguinte é a classe que faz o acesso ao Bando de Dados!
package model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import view.Painel;
public class BancoDados {
public String nome = null;
public String texto = null;
private Painel painel = new Painel();
private String entrada = new String();
public BancoDados(){
super();
String banco = "jdbc:odbc:Dicionario";
entrada = painel.acesso; //CHAMANDO A VARIAVEL DIGITADA DA CLASSE PAINEL
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(banco, "", "");
Statement st = conn.createStatement();
ResultSet rec = st.executeQuery(
"SELECT * " +
"FROM TEXTO " +
"WHERE " +
"(PALAVRA ='" + entrada + "')");
while(rec.next()){
nome = rec.getString(2);
texto = rec.getString(3);
}
st.close();
}catch (SQLException sqle) {
JOptionPane.showMessageDialog(null, "SQL Error: " + sqle.toString() + " " +
sqle.getErrorCode() + " " + sqle.getSQLState(),"Erro",
JOptionPane.ERROR_MESSAGE);
}catch (Exception e) {
JOptionPane.showMessageDialog(null,e.toString() + " " + e.getMessage(),"Erro",
JOptionPane.ERROR_MESSAGE);
}
}
}