Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/11/2010, 03:42
Wong
 
Fecha de Ingreso: julio-2009
Mensajes: 15
Antigüedad: 14 años, 9 meses
Puntos: 0
Bases de datos con Java

Buenos días a todos!!!


hoy estuve aprendiendo por primera vez a conectarme a una bd por medio de connector.. pero mi duda es... como utlizar las instrucciones para conectarme a una bd.

.....
ahora lo que me gustaría saber es como declarar para que se pueda guardar la información en la base de datos que ya cree con mysql. Estuve navegando y no encontré dichas instrucciones, pero por aqui vi unas las utilicé pero no funcionó.
Me marca los siguientes errores de sintaxis en mysql.

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:40 9)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3498)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:19 59)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionIm pl.java:2562)
at com.mysql.jdbc.StatementImpl.executeUpdate(Stateme ntImpl.java:1664)
at com.mysql.jdbc.StatementImpl.executeUpdate(Stateme ntImpl.java:1583)
at proyecto1.Formulario.btnEnviarActionPerformed(Form ulario.java:129)
at proyecto1.Formulario.access$100(Formulario.java:14 )
at proyecto1.Formulario$2.actionPerformed(Formulario. java:89)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6267)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:603 2)
at java.awt.Container.processEvent(Container.java:204 1)
at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
at java.awt.Component.dispatchEvent(Component.java:44 60)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:44 60)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

........................................

esta es toda mi clase.

package proyecto1;

import com.mysql.jdbc.Statement;
import java.sql.*;
import com.mysql.*;

/**
*
* @author Alberto Wong (tu papá)
*/



public class Formulario extends javax.swing.JFrame {

/** Creates new form Formulario */
public Formulario() {
initComponents();


setVisible(true);
setSize(400,400);
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txt1 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
txt2 = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
txt3 = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
txt4 = new javax.swing.JTextField();
btnEnviar = new javax.swing.JButton();
btnBorrar = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
getContentPane().setLayout(null);

jPanel1.setLayout(null);

jLabel1.setText("Registro de usuarios");
jPanel1.add(jLabel1);
jLabel1.setBounds(10, 20, 370, 30);

jLabel2.setText("Nombre Completo");
jPanel1.add(jLabel2);
jLabel2.setBounds(10, 80, 100, 20);

txt1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txt1ActionPerformed(evt);
}
});
jPanel1.add(txt1);
txt1.setBounds(140, 80, 230, 20);

jLabel3.setText("Dirección");
jPanel1.add(jLabel3);
jLabel3.setBounds(10, 110, 100, 20);
jPanel1.add(txt2);
txt2.setBounds(140, 110, 230, 20);

jLabel4.setText("Telefono");
jPanel1.add(jLabel4);
jLabel4.setBounds(10, 140, 90, 20);
jPanel1.add(txt3);
txt3.setBounds(140, 140, 230, 20);

jLabel5.setText("NSS");
jPanel1.add(jLabel5);
jLabel5.setBounds(10, 180, 90, 20);
jPanel1.add(txt4);
txt4.setBounds(140, 180, 230, 20);

btnEnviar.setText("Enviar");
btnEnviar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEnviarActionPerformed(evt);
}
});
jPanel1.add(btnEnviar);
btnEnviar.setBounds(10, 240, 100, 23);

btnBorrar.setText("Borrar");
btnBorrar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBorrarActionPerformed(evt);
}
});
jPanel1.add(btnBorrar);
btnBorrar.setBounds(140, 240, 120, 23);

getContentPane().add(jPanel1);
jPanel1.setBounds(0, 0, 400, 300);

pack();
}// </editor-fold>

private void txt1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void btnBorrarActionPerformed(java.awt.event.ActionEven t evt) {
// TODO add your handling code here:
borrartxt();
}

private void btnEnviarActionPerformed(java.awt.event.ActionEven t evt) {
// TODO add your handling code here:
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conexion = DriverManager.getConnection ("jdbc:mysql://localhost/alberto","root", "");
//System.out.println("Se conectó");
Statement ins = (Statement) conexion.createStatement();

Formulario obj=new Formulario();
ins.executeUpdate("INSERT INTO alberto " +
"VALUES ('"+obj.txt1.getText().toString()+obj.txt2.getText ().toString()+
obj.txt3.getText().toString()+obj.txt4.getText().t oString()) ;
} catch (Exception e)
{
e.printStackTrace();
//System.out.println(" NO se conectó");
}

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Formulario().setVisible(true);
}
});










}

// Variables declaration - do not modify
private javax.swing.JButton btnBorrar;
private javax.swing.JButton btnEnviar;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField txt1;
private javax.swing.JTextField txt2;
private javax.swing.JTextField txt3;
private javax.swing.JTextField txt4;
// End of variables declaration

public void borrartxt(){


txt1.setText(null);
txt2.setText(null);
txt3.setText(null);
txt4.setText(null);
}



}