Error: com.mysql.jdbc.Connection cannot be cast to java.sql.Statement
Hello:
I have a problem to connect to MySQL, Iâm using PreparedStatement, my IDE is Netbeans 6.5
When I run the file the error message is:
09-18-2009 03:30:13 PM tareasqlbd1.ComandosSQL btnConectarMouseClicked
GRAVE: null
java.lang.ClassCastException: com.mysql.jdbc.Connection cannot be cast to java.sql.Statement
In my Project âtareasqlbd1â I have two classes:
1)Datos.java
2)ComandosSQL.java
Code of âComandosSQL.javaâ
package tareasqlbd1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class ComandosSQL extends javax.swing.JFrame {
private Datos dt;
public ComandosSQL() {
dt= new Datos();
initComponents();
}
private void initComponents() {
btnConectar = new javax.swing.JButton();
btnEjecutar = new javax.swing.JButton();
btnDesconectar = new javax.swing.JButton();
btnAgregar = new javax.swing.JButton();
jTextFieldIdEmpleado = new javax.swing.JTextField();
jTextFieldNombres = new javax.swing.JTextField();
jTextFieldApellido1 = new javax.swing.JTextField();
jTextFieldApellido2 = new javax.swing.JTextField();
jTextFieldTelefono = new javax.swing.JTextField();
jTextFieldEmail = new javax.swing.JTextField();
}
});
btnConectar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnConectarActionPerformed(evt);
}
public void mouseClicked(java.awt.event.MouseEvent evt) {
btnDesconectarMouseClicked(evt);
}
});
//@SuppressWarnings("static-access")
private void btnConectarMouseClicked(java.awt.event.MouseEvent evt) {
try {
// TODO add your handling code here:
dt.Conectar();
JOptionPane od = new JOptionPane();
JOptionPane.showMessageDialog(this ,"Usted está conectado a la base de datos Favoritos en el servidor LocalHost");
} catch (SQLException ex) {
Logger.getLogger(ComandosSQL.class.getName()).log( Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(ComandosSQL.class.getName()).log( Level.SEVERE, null, ex);
}
}
@SuppressWarnings("static-access")
private void btnEjecutarMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
Connection cn = dt.getCn();
try {
// TODO add your handling code here:
PreparedStatement pst = cn.prepareStatement("Insert into Clientes (Idempleado,Nombres,Apellido1,Apellido2,Telefono,E mail)" +
" values (?,?,?,?,?,?);");
pst.setString(1, this.jTextFieldIdEmpleado.getText().trim());
pst.setString(2, this.jTextFieldNombres.getText().trim());
pst.setString(3, this.jTextFieldApellido1.getText().trim());
pst.setString(4, this.jTextFieldApellido2.getText().trim());
pst.setString(5, this.jTextFieldTelefono.getText().trim());
pst.setString(6, this.jTextFieldEmail.getText().trim());
pst.executeUpdate();
pst.execute("commit");
// dt.EjecutarComandoSQL(this.jtxtComandoSQL.getText( ).trim());
// JOptionPane od = new JOptionPane();
// od.showMessageDialog(this ,"Usted ejecutó el comando " + this.jtxtComandoSQL.getText().trim());
} catch (SQLException ex) {
Logger.getLogger(ComandosSQL.class.getName()).log( Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(ComandosSQL.class.getName()).log( Level.SEVERE, null, ex);
}
}
/*@SuppressWarnings("static-access")
}*/
private void btnDesconectarMouseClicked(java.awt.event.MouseEve nt evt) {
// TODO add your handling code here:
try {
// TODO add your handling code here:
dt.Desconectar();
JOptionPane od = new JOptionPane();
od.showMessageDialog(this ,"Usted se desconecto de la base de datos Favoritos en el servidor LocalHost");
} catch (SQLException ex) {
Logger.getLogger(ComandosSQL.class.getName()).log( Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(ComandosSQL.class.getName()).log( Level.SEVERE, null, ex);
}
}
private void btnConectarActionPerformed(java.awt.event.ActionEv ent evt) {
// TODO add your handling code here:
Connection cn = dt.getCn();
try {
PreparedStatement st = (PreparedStatement) cn.createStatement();
} catch (SQLException ex) {
}
}
private void btnAgregarActionPerformed(java.awt.event.ActionEve nt evt) {
}
private void jbtnAgregarMouseClicked(java.awt.event.MouseEvent evt) throws ClassNotFoundException, SQLException {
// Agregar Cliente
//java.sql.ResultSet rs=ull;
String sUsuario = null;
//Connection cn = dt.getCn();
Connection cn = dt.getCn();
try {
// TODO add your handling code here:
PreparedStatement pst = cn.prepareStatement("Insert into Clientes (Idempleado,Nombres,Apellido1,Apellido2,Telefono,E mail)" +
" values (?,?,?,?,?,?,?);");
pst.setString(1, this.jTextFieldIdEmpleado.getText().trim());
pst.setString(2, this.jTextFieldNombres.getText().trim());
pst.setString(3, this.jTextFieldApellido1.getText().trim());
pst.setString(4, this.jTextFieldApellido2.getText().trim());
pst.setString(5, this.jTextFieldTelefono.getText().trim());
pst.setString(6, this.jTextFieldEmail.getText().trim());
pst.executeUpdate();
pst.execute("commit");
} catch (SQLException ex) {
Logger.getLogger(ComandosSQL.class.getName()).log( Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ComandosSQL().setVisible(true);
}
});
}
public Datos getDt() {
return dt;
}
/**
* @param dt the dt to set
*/
public void setDt(Datos dt) {
this.dt = dt;
}
}
_________________________
I want to insert registers in my BD Clientes of the schema "Favoritos" in MySQL.
Can somebody help me please, and tell me whatâs Iâm doing wrong.
Thanksâ¦..Marielos
|