WE PROVIDE ALL

How to sort the Database Table (ordered by Job) using Collections

In this Program we are sorting based on their Job Category and Based on their Salary also. In this we taken Java Bean as object to sort the database table.We used Comparator for sorting and Employee type objects are stored in ArrayLists for Sorting.

package com.nt.finals;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class Justify {

public static ArrayList<Employ> ald = new ArrayList<Employ>();
public static ArrayList<Employ> alt = new ArrayList<Employ>();

public static void add(Employ z) {

// TODO Auto-generated method stub
alt.add(z);}


public static void add1(Employ z) {
ald.add(z);

}
public static void display()
{
Collections.sort(ald, new Comparator<Employ>() {

@Override
public int compare(Employ o1, Employ o2) {
// TODO Auto-generated method stub
if(o1.getSalary()>o2.getSalary())
return 1;
else
return -1;
}

});

for (Employ emd : ald) {

System.out.println(emd);
}
Collections.sort(alt, new Comparator<Employ>() {

@Override
public int compare(Employ o1, Employ o2) {
// TODO Auto-generated method stub
if(o1.getSalary()>o2.getSalary())
return 1;
else
return -1;
}

});

for (Employ em : alt) {

System.out.println(em);
}



}
}

public class Hazard {

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3307/organization";

// Database credentials
static final String USER = "root";
static final String PASS = "root";

public static void main(String[] args) throws SQLException,
ClassNotFoundException, IOException {
Connection conn = null;
Statement stmt = null;

try {
// STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");

// STEP 3: Open a connection
System.out.println("Connecting to database");
conn = DriverManager.getConnection(DB_URL, USER, PASS);

// STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "select id,job,salary from employ;";

ArrayList<Employ>ald=null;
ArrayList<Employ>alt=null;
ArrayList<Employ> al = new ArrayList<Employ>();
ResultSet rs = stmt.executeQuery(sql);
Justify zx = new Justify();

while (rs.next()) {
Employ z = new Employ();
int empid = (rs.getInt("id"));
String empjob = (rs.getString("Job"));
int salary = rs.getInt("salary");
z.setId(empid);
z.setJob(empjob);
z.setSalary(salary);
al.add(z);
String strs = "Tester";
String x = "Developer";
if (empjob.trim().equals(x)) {
zx.add1(z);
}

else if (empjob.trim().equals(strs)) {
zx.add(z);

}

System.out.println(z);

}
System.out.println("Sorting");
Justify.display();


}

catch (Exception e) {
e.printStackTrace();
} finally {
stmt.close();
conn.close();
}

}
}


package com.nt.finals;

public class Employ {
private int id;
private String job;
private int salary;
Employ(){

}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employ [id=" + id + ", job=" + job + ", salary=" + salary + "]";
}

}


0 comments:

Post a Comment