WE PROVIDE ALL

Using Single HashMap we can Retrieve the Entire Row from Database using any Column value

Using Single HashMap we can Retrieve the Entire Row from Database using any Column value

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 morning;

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.Scanner;


public class Until {
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);
            Scanner sc =new Scanner(System.in);
         
// STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "select id,job,salary from employ;";
ResultSet rs = stmt.executeQuery(sql);

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);
SearchEmp.add(z);
}
String key;
System.out.println("Enter the name you want to search ");
System.out.println("Enter 1. EmpId    2.Salary      3.Job");
int x=sc.nextInt();
switch(x)
{
case 1:
   System.out.println("Enter the EmpId");
      key=sc.next();
        SearchEmp.display(x,key);
        break;
     
case 2:   System.out.println("Enter the Salary ");
         key=sc.next();
                   SearchEmp.display(x,key);
                    break;
                 
case 3:       System.out.println("Enter Job");
         key=sc.next();
        SearchEmp.display(x,key);
        break;
}
}
catch(Exception e)
{
e.printStackTrace();
}

}
}

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

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


package morning;

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