rambokayambo
rambokayambo

Reputation: 341

update sql statement not working

i have this code

earthid is the primary key and this is what i use to make sure the exact row is updated.

int row=jTable1.getSelectedRow();
String sql="UPDATE TB_EARTHORIENTATIONPARAMETER SET  year="+year1+",        month="+month1+",  day="+day1+",  mjd="+mjd1+",ut1utcdelta="+Ut1utc+" WHERE (earthid="+row1+")";

yet when i execute this statement nothing happens....the database does not get updated with the new values i cannot figure out whats wrong with my sql update statement

ok here is more of my code that is behind my update button.

jTable1.getModel().addTableModelListener(jTable1);
    DefaultTableModel model=(DefaultTableModel)jTable1.getModel();

    int row=jTable1.getSelectedRow();
    int row1=row+1;
    int column=0;
    String years="";
    String months="";
    String days="";
    String mjds="";
    String ut1utc="";
    int year1;
    int month1;
    int day1;
    double mjd1;
           double Ut1utc;


    while(column!=7)
    {

        Object year=model.getValueAt(row, column);
        years=year.toString();
        year1=Integer.parseInt(years);

        column++;
        Object Month=model.getValueAt(row, column);
        months=Month.toString();
        month1=Integer.parseInt(months);
        column++;
        Object Day=model.getValueAt(row, column);
        days=Day.toString();
        day1=Integer.parseInt(days);
        column++;
        Object MJD=model.getValueAt(row, column);
        mjds=MJD.toString();
        mjd1=Double.parseDouble(mjds);
        column++;
        Object UT1UTCDELTA=model.getValueAt(row,column);
        ut1utc=UT1UTCDELTA.toString();
        Ut1utc=Double.parseDouble(ut1utc);
        column++;

    try
    {
     DBConnectionclass cm=new DBConnectionclass(); 
     String sql="UPDATE TB_EARTHORIENTATIONPARAMETER SET  year="+year1+",  month="+month1+",  day="+day1+",  mjd="+mjd1+",ut1utcdelta="+Ut1utc+" WHERE (earthorientationparameterid="+row1+")";
     Connection con1=null;
     Statement stmt=null;
     con1=cm.getConnection();    //get a connection object
     stmt=con1.createStatement();
     stmt.executeUpdate(sql);

     //create an instance of the database connection class to create a connection object to beused to conect to database

     //create a statement to process sql query

    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
    // TOD




    }

its not the best way to write code but it worked just fine with mysql. Do i need to do a select for update

Upvotes: 0

Views: 1817

Answers (4)

rambokayambo
rambokayambo

Reputation: 341

hey guys i solved the problem. This is my fault totally. I when i select and return a record set from the database i did not explicitly specifiy which order it would come back in. Hence my record set was out of order and so it wasperforming an update but i it wasnt in order because my update explicitly updated by earthorientationparametersid. It had nothing to do with the update but with the order in which my grid displayed data and thats why i couldnt see an update

Upvotes: 0

Cashley
Cashley

Reputation: 516

Your date parameters in the string should have single quotes around them. I assume earthid is an int and not a GUID as well, or that will need single quotes as well. Have you tried a parametrised query? Can you post a larger code sample?

Upvotes: 0

Brock B.
Brock B.

Reputation: 377

Try putting single quotes around your values.

String sql="UPDATE TB_EARTHORIENTATIONPARAMETER SET  year='"+year1+"', month='"+month1+"', day='"+day1+"', mjd='"+mjd1+"',ut1utcdelta='"+Ut1utc+"' WHERE (earthid='"+row1+"')";

Upvotes: 1

Karna Desai
Karna Desai

Reputation: 61

Have you tried to use single quotes inside the SQL query? .I have edited your query give it a try

String sql="UPDATE TB_EARTHORIENTATIONPARAMETER SET  year='+year1+',        month='+month1+',  day='+day1+',  mjd='+mjd1+',ut1utcdelta='+Ut1utc+' WHERE (earthid='+row1+')";

Also are you getting any exception?

Upvotes: 1

Related Questions