I want to update some values within a column based on different situations…..
the table has the following details like…..
Date date
period int
subcode varchar(3)
status_bits varchar(100)
// status bits resemble an information based code based...the data usually stored are 2343211 where each value in digits represent an information.....
Now i’ve got to update these values based on different dates and periods….
Now considering the java program……
i have stored the details in corresponding variables in java like..
java.util.Date date[];
int period[];
String subcode[];
// Here for an index i , they share the same values within a row.....
If i wanna update it in such a way that that i want to change the 5th letter in a varchar for different dates,periods and subcodes(combined)…..
now..i’ve currently performed like this…
Connection con;
preparedStatement ps;
String bitstatus;
for(i=0; i < noofupdates; i++)
{
ps = con.prepareStatement("select status_bits from tablename where Date = ? AND period = ? AND subcode = ? limit 0,1");
ps.setDate(1,date[i]);
ps.setInt(2,period[i])'
ps.setString(3,subcode[i]);
rs=ps.executeQuery();
while(rs.next())
{
bitstatus = rs.getString(1);
// performed operation to update the bit.....
ps=con.prepareStatment("update status_bits correspondind to the same date,field and subcode");
ps.executeUpdate();
}
}
Now i presume you understand from the program that i want to update status_bits in the table based on different dates and periods where the bit operation is common……Now i really know that these method is hammering lots of queries and seriously affecting mysql performance…So plz help me by providing a much alternate idea to this…….there are around 1000 records to update………
If you know you want the 5th position of the status_bits string set to a particular value for a given (date,period,subcode), you can avoid the SELECT and perform just an update.
UPDATED: to properly handle cases where the existing value in status_bits column is less than 4 characters, we want to ensure we are replacing the 5th character, and not the 2nd character when the existing string is only 1 character long. We can use a CASE expression to do the test. When we have at least 4 characters, we can append our (desired) 5th character after the first four characters. Otherwise, we need to “pad out” the existing string to 4 characters, before we append our (desired) 5th character. (Previously, the statement only included the expression that worked for existing strings at least 4 characters in length.)
(You can add the LIMIT clause if you don’t want to update all matching rows, but this isn’t needed if you want to update all matching rows. If
(date,period,subcode)is unique, you will match at most one row.)It’s also possible to set the 5th position of status_bits dependent on other conditions, for example, by adding predicates to the where clause, or a CASE expression (or IF function) in the SET.
is equivalent to:
This approach will cut in half the number of queries you are running (reduces the number of roundtrips to the database) and avoids you having to return the entire
status_bitscolumn.(Unfortunately, the SQL text to replace the 5th character of a varchar column is a bit unwieldy, since MySQL doesn’t have the more compact
STUFFfunction that is provided by SQL Server.)Update: more complex expression required to handle existing status_bits strings that are less than 4 characters in length…)
NOTE: In that SQL statement, the value of the character to be placed in the 5th position will need to be supplied in two separate positional parameters.
NOTE: