WE PROVIDE ALL

Retrieve the data from the Database and Sort the Data Based on Job corresponding to Salary using Collections


Retrieve the data from the Database and Sort the Data Based on Job corresponding to Salary using Collections 


Using this program we can get the entire row by giving any attribute value of that row by using only one Single HashMap and JavaBean. In  this program we used the JDBC connection  to the Database MySQL. we can alter the database table values using Collections.


package com.nt.show;

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> all = new ArrayList<Employ>();

public static void add(Employ z) {
all.add(z);

}

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

@Override
public int compare(Employ o1, Employ o2) {
return o1.getJob().compareTo(o2.getJob());
}

});

for (Employ emd : all) {
System.out.println(emd);
}

}

public static void mySort(final String s) {

Collections.sort(all, new Comparator<Employ>() {
@Override
public int compare(Employ o1, Employ o2) {

if (o1.getSalary() > o2.getSalary()&&(o1.getJob().trim().equals(s)))
return 1;
else
return -1;
                     
}
});
System.out.println(" also Started");
for (Employ ex : all) {

System.out.println(ex);

}
}



}

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> al = new ArrayList<Employ>();
ArrayList<Employ> all = new ArrayList<Employ>();
ResultSet rs = stmt.executeQuery(sql);
Justify zx = new Justify();
Employ z = null;
while (rs.next()) {
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);

Justify.add(z);

System.out.println(z);

}
System.out.println("Sorting");
long lStartTime = System.currentTimeMillis();
Justify.display();
String y="Tester";
String x="Developer";
Justify.mySort(x);
Justify.mySort(y);

long lEndTime = System.currentTimeMillis();
long difference = lEndTime - lStartTime;

System.out.println("Elapsed milliseconds: " + difference);

}

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

}



}
Using the Java Bean we can get the entire user defined set of attributes(datatypes) under single object.

  1. A Java Bean Must have Setter and Getter Methods
  2. It must be implemented by serializable.
  3. It must have a Constructor.

package com.nt.show;

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