JDBC 4: (WHERE CLAUSE, INSERTING, RETREIVING)
public class User {
private Long id;
private String name;
private String mobileNumber;
private String username;
private String password;
public User(){}
public User(Long id,String name, String mobileNumber, String username, String password){
this.id=id;
this.name=name;
this.mobileNumber=mobileNumber;
this.username=username;
this.password=password;
}
public User(Long id) {
this.id=id;
}
public Long getId()
{
return this.id;
}
public String getName(){
return this.name;
}
public String getMobileNumber(){
return this.mobileNumber;
}
public String getUsername(){
return this.username;
}
public String getPassword(){
return this.password;
}
public void setId(Long id)
{
this.id=id;
}
public void setName(String name){
this.name=name;
}
public void setMobileNumber(String mobileNumber){
this.mobileNumber=mobileNumber;
}
public void setUsername(String username){
this.username=username;
}
public void setPassword(String password){
this.password=password;
}
}
______________________________________________________________
public class Hall {
private Long id;
private String name;
private String contactNumber;
private Double costPerDay;
private User owner;
public Hall(){}
public Hall(String name, String contactNumber, Double costPerDay, User owner){
this.name=name;
this.contactNumber=contactNumber;
this.costPerDay=costPerDay;
this.owner=owner;
}
public Long getId()
{
return this.id;
}
public String getName(){
return this.name;
}
public String getContactNumber(){
return this.contactNumber;
}
public Double getCostPerDay(){
return this.costPerDay;
}
public User getOwner(){
return this.owner;
}
public void setId(Long id)
{
this.id=id;
}
public void setName(String name){
this.name=name;
}
public void setContactNumber(String contactNumber){
this.contactNumber=contactNumber;
}
public void setCostPerDay(Double costPerDay){
this.costPerDay=costPerDay;
}
public void setOwner(User owner){
this.owner=owner;
}
}
_____________________________________________________________________________
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UserDAO {
public User getUser(String username) throws ClassNotFoundException, SQLException {
User user = null;
Connection conn = DBConnection.getConnection();
Statement stmt = conn.createStatement();
String query = "select * from \"user\" where username ='"+username+"'";
ResultSet rs = stmt.executeQuery(query);
if(rs.next()) {
Long id = rs.getLong(1);
String name = rs.getString(2);
String contact_detail = rs.getString(3);
String uname = rs.getString(4);
String pwd = rs.getString(5);
user = new User(id,name,contact_detail,uname,pwd);
}else {
user=null;
}
return user;
}
public User getUser2(Long id) throws ClassNotFoundException, SQLException {
User user = null;
Connection conn = DBConnection.getConnection();
Statement stmt = conn.createStatement();
String query = "select * from \"user\" where id ='"+id+"'";
ResultSet rs = stmt.executeQuery(query);
if(rs.next()) {
Long i_d = rs.getLong(1);
String name = rs.getString(2);
String contact_detail = rs.getString(3);
String uname = rs.getString(4);
String pwd = rs.getString(5);
user = new User(i_d,name,contact_detail,uname,pwd);
}else {
user=null;
}
return user;
}
}
_______________________________________________________________________________
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class HallDAO {
public void saveHall(Hall hall) throws ClassNotFoundException, SQLException {
Connection conn = DBConnection.getConnection();
String query = "insert into hall (name,contact_detail,cost_per_day,owner_id) values(?,?,?,?)";
PreparedStatement pr = conn.prepareStatement(query);
pr.setString(1, hall.getName());
pr.setString(2, hall.getContactNumber());
pr.setDouble(3, hall.getCostPerDay());
pr.setLong(4, hall.getOwner().getId());
pr.executeUpdate();
conn.commit();
pr.close();
}
public List<Hall> getAllHall() throws SQLException, ClassNotFoundException {
List<Hall> hallList = new ArrayList<Hall>();
Connection conn = DBConnection.getConnection();
Statement st = conn.createStatement();
String query = "select * from hall order by id";
ResultSet rs = st.executeQuery(query);
while(rs.next()) {
String nm = rs.getString(2);
String con = rs.getString(3);
Double cost = rs.getDouble(4);
Long owner_id = rs.getLong(5);
UserDAO dao2 = new UserDAO();
User u = dao2.getUser2(owner_id);
Hall hall = new Hall(nm,con,cost,u);
hallList.add(hall);
hall=null;
}
return hallList;
}
}
_______________________________________________________________________________
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 conn = DriverManager.getConnection(url,username,password);
return conn;
}
}
_______________________________________________________________________________
import java.lang.reflect.Array;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the details of hall in csv format:");
String inp = sc.nextLine();
String arr[]=inp.split(",");
HallDAO dao1 = new HallDAO();
System.out.println("Enter the username:");
String a="y";
while(a=="y") {
String username = sc.nextLine();
UserDAO dao2 = new UserDAO();
User u = dao2.getUser(username);
if(u!=null) {
//User user = new User(u.getId());
Hall hall = new Hall(arr[0],arr[1],Double.parseDouble(arr[2]),u);
dao1.saveHall(hall);
a="n";
}else {
System.out.println("Username seems to be wrong!! Enter the correct username:");
}
}
HallDAO dao3 = new HallDAO();
List<Hall> hallList = new ArrayList<Hall>();
hallList = dao3.getAllHall();
System.out.println("The hall details are:");
System.out.format("%-15s%-15s%-15s%-15s\n","Name","Mobile","Cost","Owner");
for (Hall h : hallList) {
System.out.format("%-15s%-15s%-15s%-15s\n", h.getName(), h.getContactNumber(), h.getCostPerDay(), h.getOwner().getName());
}
}
}
Comments
Post a Comment