Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 18th, 2009, 07:44 PM
Registered User
 
Join Date: Sep 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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
 
Old September 19th, 2009, 03:03 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 227
Thanks: 1
Thanked 7 Times in 7 Posts
Default

Marielos_Cornejo:

Looking at your code there seems to be a problem with balancing the braces and the parentheses.


Code:
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();
}
});
The brace and close-paenthesis ")" after the closed-brace "}" placed before the semi-colon ";" is used throughout your code. Remember for every open braces and/or parentheses there should be an equal number of closed braces and/or parentheses.

Hope this helps.
__________________
Disclaimer: The above comments are solely the opinion of one person and not to be construed as a directive or an incentive to commit fraudulent acts.
The Following User Says Thank You to peace95 For This Useful Post:
Marielos_Cornejo (September 21st, 2009)
 
Old September 21st, 2009, 01:26 AM
Registered User
 
Join Date: Sep 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Thumbs up Thanks you peace95...!!!

Thanks peace95....I considered your comments....and really worked..!!

Thanks..again





Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection Problem with Java JDBC dontknowmuch SQL Server 2005 3 April 4th, 2006 01:51 PM
Execute mySQL statement in Java? DevlshOne MySQL 1 March 16th, 2005 12:45 PM
java.sql.SQLException: Error loading JDBC Driver jdphjp Java Databases 1 August 5th, 2004 12:46 AM
Cannot load JDBC driver class with com.mysql.jdbc. dzis Apache Tomcat 0 January 29th, 2004 11:57 AM
Cannot load JDBC driver class with com.mysql.jdbc. dzis MySQL 0 January 29th, 2004 11:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.