🌙 DARK

JDBC Without Collections

 Select Statement

  

Write a program to retrieve all the records present in the User table and display those records in the specified format using the SELECT select.

Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, attribute names and method names should be the same as specified in the problem statement.


Create a class User with the following private attributes

AttributesDatatype
idLong
nameString
contactDetailString
usernameString
passwordString

Generate appropriate Getters and Setters
Generate default and parameterized constructors
The parameterized constructor should be in the following format. 
User(Long id,String name, String contactDetail, String username, String password)

 
Create a class UserDAO with the following methods
Method Description
public void displayUsers()This function finds all records of the user table and display them in the given format

 

Create a class DBConnection with the following static methods,

MethodDescription
static Connection getConnection()this method uses "oracle.properties" file as a resource file and
gets connection object for the MySQL database and returns the connection object.

 

Create a driver class Main to call the methods of above class.
 

NOTE : Use System.out.format("%-5s %-5s %-15s %-10s %s\n","Id","Name","Contact Detail","Username","Password") to display user details.

Table properties:

CREATE  TABLE “user”(
id number(19) NOT NULL,
name VARCHAR2(45) NOT NULL,
contact_detail VARCHAR2(45) NOT NULL,
username VARCHAR2(45) NOT NULL,
password VARCHAR2(45) NOT NULL,
PRIMARY KEY (id));
CREATE SEQUENCE user_seq START WITH 1 INCREMENT BY 1;


Use the following code snippet to establish DBConnection:
import java.util.ResourceBundle;
ResourceBundle rb = ResourceBundle.getBundle("oracle");
String url = rb.getString("db.url");
String username = rb.getString("db.username");
String password = rb.getString("db.password");

oracle.properties:
db.url = jdbc:oracle:thin:@localhost:1521:xe
db.username = root
db.password = student
 
Download the oracle jar file in the below link.
Oracle jar
 
[All text in bold corresponds to the input and rest corresponds to the output]
Sample Input and Output:
 
IdNameContact DetailUsernamePassword
1John9876543210johny12345
2Peter9873216540petereypet123
3Adam9871236504adamantaad@123
4Linda8794561320lindahereabcd
5Tony7894561230toniiabc123

 


@MRProgrammer89 


CREATE CLASS < User:




public class User {
//your code goes here...
    private Long id;
    private String name;
    private String contactDetail;
    private String username;
    private String password;
    
    public User(){}
    public User(Long id,String name,String contactDetail,String username,String password){
        this.id = id;
        this.name  = name;
        this.contactDetail = contactDetail;
        this.username = username;
        this.password = password;
    }
    
    public Long getId(){
        return id;
    }
    public void setId(Long id){
        this.id = id;
    }
    public String getName(){
        return name;
    }
    public void setname(String name){
        this.name = name;
    }
    public String getContactDetail(){
        return contactDetail;
    }
    public void setContactDetail(String contactDetail){
        this.contactDetail = contactDetail;
    }
    public String getUsername(){
        return username;
    }
    public void setUsername(String username){
        this.username = username;
    }
    public String getPassword(){
        return password;
    }
    public void setPassword(String password){
        this.password = password;
    }
}





@MRProgrammer89 


CREATE CLASS < UserDAO:






import java.sql.*;
public class UserDAO {
    public void displayUsers() throws SQLException{
        try{
        DBConnection con = new DBConnection();
        
        Connection conn = con.getConnection();
        Statement stm = conn.createStatement();
        String sql = "select * from \"user\" ";
        ResultSet rs = stm.executeQuery(sql);
        
        System.out.format("%-5s %-5s %-15s %-10s %s\n","Id","Name","Contact Detail","Username","Password");
        while(rs.next()){
            Long id = (long)rs.getInt(1);
            String name =rs.getString(2);
            String contactDetail =rs.getString(3);
            String username = rs.getString(4);
            String password = rs.getString(5);
            
            User obj = new User(id,name,contactDetail,username,password);
            
            System.out.format("%-5s %-5s %-15s %-10s %s\n",obj.getId(),obj.getName(),obj.getContactDetail(),obj.getUsername(),obj.getPassword());
        
        
        }
    }catch(ClassNotFoundException e){
    System.out.println(e);   
}
        
    }}



@MRProgrammer89 


CREATE CLASS < DBConnection :





import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class DBConnection {
public static Connection getConnection() throws ClassNotFoundException, SQLException {        
        ResourceBundle rb = ResourceBundle.getBundle("oracle");
        String url = rb.getString("db.url");
        String username = rb.getString("db.username");
        String password = rb.getString("db.password");
        Connection con = DriverManager.getConnection(url,username,password);
        
        return con;
    }
}



@MRProgrammer89 


CREATE CLASS < Main :





import java.sql.*;
public class Main {
    public static void main(String args[]) throws SQLException{
        UserDAO obj = new UserDAO();
        obj.displayUsers();
}
}




@MRProgrammer89 






MOTIVATION :


    “The way we spend our time defines who we are 


Although we may not see each other as often as we’d like, distance is no match for the bond that we share. Thank you for coming to visit. It was fantastic to catch up.


VISITE MORE BLOGS




THANK YOU....😊




Post a Comment

Thanks for reading the blog. We hope it was useful to you and that you learned something new. Will always be writing on new and interesting topics, so you can visit our website to know the latest updates of our blogs. Thank You!

Previous Post Next Post

Contact Form