If you want get all the distinct values from any column of your database table using java. You have to take all the columns in the ResultSet object and then pass that ResultSet object to these methods.You have to take data into Resultset Object like this.
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);ResultSet rs=stmt.executeQuery("Select * from Table_Name");
You must be knowing the rest code. You have to encapsulate the following code into a class. After that, from Main Class you have to call these methods passing the Resultset Object rs.
int counter=0;int count1=0;int deptno[],A[],B[];float Avg[];////This method will find the number of rows in the resultsetvoid countRow(ResultSet rs) throws Exception{while(rs.next()){counter++;}}////This will put distinct value inside an array deptno[count]void distinctValue(ResultSet rs) throws Exception{deptno=new int[counter];A=new int[counter];B=new int[counter];int count=0,i=-1,x=0,y;boolean bool=true;rs.beforeFirst();while(rs.next()){i++;deptno[i]=rs.getInt(8);//--Here you have to give the column numberA[i]=deptno[i];}while(bool){y=-1;deptno[count]=A[x];for(int j=0;j{if(deptno[count] != A[j]){y++;B[y]=A[j];}}counter=y+1;if(y==-1){count1=count;break;}for(int j=0;j<=y;j++){A[j]=B[j];if(j==0){count++;deptno[count]=A[j];}}}}
The above code will give you distinct values of any numbered column into a array deptno[count]. And the size of array will be Zero-count1. Also you have to define the ResultSet as TYPE_SCROLL_SENSITIVE.
deptno[i]=rs.getInt(8); Here you have to give your desired column number. In my case its 8'th column.
0 comments:
Post a Comment