Google

Sunday, July 12, 2009

MySQL: Decimal, Nested Query, ResultSet Pointer Manipulation

FLOAT:
FLOAT(4,2) will give 1.1 for data input 1.1


DECIMAL:
DECIMAL(4,2) will give 1.10 for data input 1.1
4 means 4 digits exclusing decimal point.
eg. 12.34 is four digits.
2 means how many decimal places.

So use decimal.

To get decimal from a ResultSet:
double f = resultSet.getDouble("credithour");


assuming credithour is a DECIMAL(10,2)
you will get 12.34

To input a decimal:
double value = Double.parseDouble((String)table.getValueAt(rowl,col));
Then, use the value in an SQL statement:

INSERT INTO table(name, marks) VALUES('James',value);

Example of a Nested Query:
select * from psubject
where pid in ( select pid from program where name='DICT' )
AND sem=1;

ResultSet Pointer:
After every update or query, the ResultSet points to the location
before the first row. You need to do this before getting anything
out again:

if(rsPsubject.next()) populateSubjectListForm();

Moving ResultSet to updated row (after an update):
After doing any updates, your ResultSet points to location
before the firt row. If you wish to go back to the previous
row, extract the primary key first before doing the update,
the do the update, then use a simple loop to get back to
the previous row:

//---get the primary key of the current row---
int currentStudentID = rsStudent.getInt("studentid");



//---Do your update here:



requeryStudent();

//---go back to row before the update---
rsStudent.next();
while(!(rsStudent.getInt("studentid")==currentStudentID)) rsStudent.next();