Results 1 to 5 of 5
- 06-10-2011, 07:20 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
Java/MySQL UPDATE.........Help Please
package projet.bdd;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Map;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
@SuppressWarnings("serial")
public class ModifierEtudiant extends JFrame {
private DataPanel myDataPanel;
Connection conn= new Connection() {
@Override
public <T> T unwrap(Class<T> arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isWrapperFor(Class<?> arg0) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public void setTypeMap(Map<String, Class<?>> arg0) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setTransactionIsolation(int arg0) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public Savepoint setSavepoint(String arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Savepoint setSavepoint() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setReadOnly(boolean arg0) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setHoldability(int arg0) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setClientInfo(String arg0, String arg1)
throws SQLClientInfoException {
// TODO Auto-generated method stub
}
@Override
public void setClientInfo(Properties arg0) throws SQLClientInfoException {
// TODO Auto-generated method stub
}
@Override
public void setCatalog(String arg0) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setAutoCommit(boolean arg0) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void rollback(Savepoint arg0) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void rollback() throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void releaseSavepoint(Savepoint arg0) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public PreparedStatement prepareStatement(String arg0, int arg1, int arg2,
int arg3) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String arg0, int arg1, int arg2)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String arg0, String[] arg1)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String arg0, int[] arg1)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String arg0, int arg1)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public CallableStatement prepareCall(String arg0, int arg1, int arg2,
int arg3) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public CallableStatement prepareCall(String arg0, int arg1, int arg2)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public CallableStatement prepareCall(String arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public String nativeSQL(String arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isValid(int arg0) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isReadOnly() throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isClosed() throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public SQLWarning getWarnings() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public int getTransactionIsolation() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public int getHoldability() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getClientInfo(String arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Properties getClientInfo() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public String getCatalog() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean getAutoCommit() throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public Struct createStruct(String arg0, Object[] arg1) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Statement createStatement(int arg0, int arg1, int arg2)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Statement createStatement(int arg0, int arg1) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Statement createStatement() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public SQLXML createSQLXML() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public NClob createNClob() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Clob createClob() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Blob createBlob() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Array createArrayOf(String arg0, Object[] arg1) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void commit() throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void close() throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void clearWarnings() throws SQLException {
// TODO Auto-generated method stub
}
};
private static String info;
private static JTextArea txtInfo = new JTextArea(20,10); // 20 lignes 10 cols
//*******************across classes************************///
public ModifierEtudiant() {
super("Modifier Des Etudiants");
GridLayout myGridLayout = new GridLayout(3, 1); //*****3 lignes 1 colone pour les 2 paries Pannels****////
Container p = getContentPane();
myDataPanel = new DataPanel();
p.add(myDataPanel);
myDataPanel.setLayout(myGridLayout);
// **********************************
txtInfo.setText(info); // sets connection information
setSize(400,500);
setVisible(true);
}
public static void main(String args[]) {
ModifierEtudiant Fenetre = new ModifierEtudiant();
Fenetre.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
// *******************************LES LABELSSSSS**********************/////
class DataPanel extends JPanel implements ActionListener { // QUOI ECRIRE//
JLabel lblCode = new JLabel("Code :");
JLabel lblNom = new JLabel("Nom :");
JLabel lblPrenom = new JLabel("Prenom :");
JLabel lblDateNaiss = new JLabel("Date Naissance :");
JLabel lblSpec = new JLabel("Specialité :");
JLabel lblSec = new JLabel("Section :");
///***************J FORMATTED TEXT FIELD****************//////////
JTextField txtCode = new JTextField();
JTextField txtNom = new JTextField();
JTextField txtPrenom = new JTextField();
JTextField txtDateNaiss = new JFormattedTextField(DateFormat
.getDateInstance());
JTextField txtspec = new JTextField();
JTextField txtsec = new JTextField();
JButton btnModifier = new JButton("Modifier");
JButton btnQuitter = new JButton("Quitter");
public DataPanel() {
JPanel myPanel = new JPanel();
myPanel.setBackground(Color.red); //****************couleur premier parie/////
JPanel myPanel2 = new JPanel();
try {
MaskFormatter datenaiss = new MaskFormatter("19##-##-##");
txtDateNaiss = new JFormattedTextField(datenaiss);
} catch (ParseException e) {
e.printStackTrace();
}
myPanel.setLayout(new GridLayout(6, 2)); // 6 lignes 2 colones
myPanel2.setLayout(new GridLayout(2, 1)); // 2 lignes 1 colones
add(myPanel);
add(myPanel2);
add(txtInfo);
// **
myPanel.add(lblCode);// premiere partie//
myPanel.add(txtCode);
myPanel.add(lblNom);
myPanel.add(txtNom);
myPanel.add(lblPrenom);
myPanel.add(txtPrenom);
myPanel.add(lblDateNaiss);
myPanel.add(txtDateNaiss);
myPanel.add(lblSpec);
myPanel.add(txtspec);
myPanel.add(lblSec);
myPanel.add(txtsec);
// seconde partie//pour les bouttons////////////
myPanel2.add(btnModifier);
myPanel2.add(btnQuitter);
//*********************TOUT LES BOUTTONS********************//
btnModifier.addActionListener(this);
btnQuitter.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
String code="";
String nom="";
String prenom="";
String spec="";
String sec="";
Object source = event.getSource();
@SuppressWarnings("unused")
Timestamp sqlDate = new java.sql.Timestamp(new java.util.Date()
.getTime());
code = txtCode.getText().trim(); // removes additional characters
txtCode.setText(code); // sets fields in their places
nom = txtNom.getText().trim();
txtNom.setText(nom);
prenom= txtPrenom.getText().trim();
txtPrenom.setText(prenom);
spec = txtspec.getText().trim();
txtspec.setText(spec);
sec = txtsec.getText().trim();
txtsec.setText(sec);
// ****************************//BOUTON Chercher//***********************************///
if (source.equals(btnModifier))
{
try
{
Statement statement = conn.createStatement();
if (!code.equals(" ") )
{
conn = DriverManager
.getConnection("jdbc:mysql://localhost/base de donnee?"
+ "user=root&password=root");
conn.setAutoCommit(false);
PreparedStatement ps=conn.prepareStatement("UPDATE etudiant SET "
+"nom='" + txtNom.getText()
+"prenom='" + txtPrenom.getText()
+" date_naiss='" + txtDateNaiss.getText()
+"specialité : "+ txtspec.getText()
+"Section : "+ txtsec.getText()
+ "WHERE code_etud= '" + code + "'");
ps.setString(2, nom);
ps.setString(3, prenom);
ps.setString(5, spec);
ps.setString(6, sec);
txtInfo.setText("Code Etudiant : " + txtCode.getText()
+"\n "+"Nom : " + txtNom.getText()
+"\n "+"Prenom : " + txtPrenom.getText()
+"\n "+"Date naissance : " + txtDateNaiss.getText()
+"\n "+"code specialité : " +txtspec.getText()
+"\n "+"code section : "+ txtsec.getText()+"\n"+
"Succes");
ps.executeUpdate();
statement.close();
}
else
txtInfo.append( "eeeeeeeeee" );
}
catch ( SQLException sqlex ) {
txtInfo.append( sqlex.toString() );
} }
// ********************************************// BOUTTON
// QUITTER //
if (source.equals(btnQuitter)) {
System.exit(0);
}
}
// ********************************************
public void display(ResultSet rs) {
if (!(txtCode==null)) {
txtInfo.append("\n*** Etudiant Non Existant***\n");
}
}
}}
- 06-10-2011, 08:11 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Help with what? You've posted a huge amount of code (and please use the code tags to make them readable) without a defined question...what is the problem. Does it compile? Are there exceptions when ran? Does it behave? Please post all error messages in their entirety.
- 06-10-2011, 08:28 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 0).
and sometimes I get on the "txtinfo" input from "jtextfield" but in the database nothing changes...
Last edited by Lamita Nera; 06-10-2011 at 08:31 PM.
- 06-10-2011, 09:02 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at projet.bdd.ModifierEtudiant$DataPanel.actionPerfor med(ModifierEtudiant.java:512)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 06-10-2011, 09:13 PM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Similar Threads
-
update arraylist to mysql database
By sks in forum JDBCReplies: 11Last Post: 04-01-2011, 08:26 AM -
Update New to Java
By Jaguar1998 in forum New To JavaReplies: 9Last Post: 04-09-2010, 11:54 AM -
update a change in a Jtable directly to the mysql table
By Muffel in forum JDBCReplies: 0Last Post: 02-21-2010, 11:51 AM -
Mysql/JDBC update query problem
By thelinuxguy in forum Advanced JavaReplies: 3Last Post: 02-11-2009, 09:56 PM -
unable to update MYSQL with values from jsp page
By koushika in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 03-27-2008, 01:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks