Estou com uma dúvida de como conectar um applet em um banco.
Pelo menos este não está dando certo não.
Vejam o código:
[code]import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
public class AppletJDBCDrop extends JApplet implements ActionListener {
private Connection connection;
private JList tableList;
private JButton dropButton;
public void init() {
Connection connection;
try {
Class.forName(“org.gjt.mm.mysql.Driver”).newInstance();
//ou com.mysql.jdbc.Driver
connection =
DriverManager .getConnection(“jdbc:mysql://localhost/banco?user=root&password=2017”);
JOptionPane.showMessageDialog(null,“Conectado!”);
} catch (Exception connectException) {
JOptionPane.showMessageDialog(null,"Não conectado!",
"", JOptionPane.ERROR_MESSAGE);
connectException.printStackTrace();
}
Container c = getContentPane();
tableList = new JList();
//loadTables();
c.add(new JScrollPane(tableList), BorderLayout.NORTH);
dropButton = new JButton("select Table");
dropButton.addActionListener(this);
c.add(dropButton, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(“select * from db;”
+ tableList.getSelectedValue());
} catch (SQLException actionException) {
}
}
private void loadTables() {
Vector v = new Vector();
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SHOW db");
while (rs.next()) {
v.addElement(rs.getString(1));
}
rs.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Não deu certo!",
"", JOptionPane.ERROR_MESSAGE);
}
v.addElement("acc_acc");
v.addElement("acc_add");
v.addElement("junk");
tableList.setListData(v);
}
}
[/code]
Veja o que acontece toda vez:
Espero que possam me ajudar.
Obrigado.