2015-12-21 2 views
1

J'ai essayé d'exécuter le code suivant dans un programme Java pendant un certain temps à partir d'un bouton. J'ai tout essayé mais il y a toujours une exception. J'ai déjà fait la table et y ai mis un seul disque.MySql Java Connectivity throws exception. (UPDATE)

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { 
    // TODO add your handling code here: 
try{ 
    Class.forName("java.sql.Driver"); 
    String UID="root"; 
    String PWD="123"; 
    String DB_URL="jdbc:mysql://localhost:3306/project"; 
    Connection conn=DriverManager.getConnection(DB_URL,UID,PWD); 

    String CatalogID=jTextField3.getText(); 
    String Title=jTextField4.getText(); 
    String Author=jTextField5.getText(); 
    String Price=jTextField6.getText(); 
    String Category=jTextField7.getText(); 

    Statement stmt=conn.createStatement(); 
    String q="UPDATE table1 SET title='"+Title+"' author='"+Author+"' category='"+Category+"' price="+Price+" WHERE id="+CatalogID+";"; 
    stmt.executeUpdate(q); 
} 

catch(Exception e){ 
JOptionPane.showMessageDialog(null,"Error"); 
} 


} 

Répondre

1

vous forgott la , entre vos colonnes:

String q="UPDATE table1 SET title='"+Title+"', author='"+Author+"', category='"+Category+"', price="+Price+" WHERE id="+CatalogID+";"; 

btw. il est préférable d'utiliser PreparedStatement. Vous ouvrez une porte pour les attaques par injection SQL.

+0

Essayé, le problème persiste ' –

+0

Juste un projet d'école. Nous venons de commencer à apprendre sql. –

+0

quelle exception obtenez-vous exactement? –