Last active
January 17, 2017 11:40
-
-
Save sandeepmchouhan111293/485a6436391ef5f749f4535b9c8d09ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.util.Properties; | |
public class MyMain { | |
public static void main(String[] args) | |
{ | |
try | |
{ | |
FileInputStream fileRead = new FileInputStream("oracle.properties"); | |
Properties prop=new Properties(); | |
prop.load(fileRead); | |
String jname = prop.getProperty("username"); | |
String jpass = prop.getProperty("password"); | |
System.out.println("Username:"+jname+"Password:"+jpass); | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
System.out.println("Error in reading..."); | |
} | |
} | |
} | |
=============================================================================================================== | |
oracle.properties | |
username=Lab08trg1 | |
password=Lab08oracle | |
=============================================================================================================== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.util.Properties; | |
import java.util.Scanner; | |
public class MyTest { | |
public static void main(String[] args) | |
{ | |
try { | |
FileInputStream fileRead= | |
new FileInputStream("oracle.properties"); | |
Properties pros =new Properties(); | |
pros.load(fileRead); //Load Properties File | |
String driver=pros.getProperty("oracle.driver"); | |
String url=pros.getProperty("oracle.url"); | |
String uname=pros.getProperty("oracle.username"); | |
String upass=pros.getProperty("oracle.password"); | |
//Load The Driver | |
Class.forName(driver); | |
//Making Connection | |
Connection conn= | |
DriverManager.getConnection(url, uname, upass); | |
System.out.println("Connection Established..."); | |
String query="SELECT prod_id,prod_name,prod_price,prod_des FROM PRODUCTDB"; | |
PreparedStatement pstm=conn.prepareStatement(query); | |
ResultSet res=pstm.executeQuery(); | |
while(res.next()) | |
{ | |
System.out.println(res.getInt("prod_id")); | |
System.out.println(res.getString("prod_name")); | |
System.out.println(res.getDouble("prod_price")); | |
System.out.println(res.getString("prod_des")); | |
} | |
} | |
catch ( ClassNotFoundException | SQLException | IOException e) | |
{ | |
e.printStackTrace(); | |
System.out.println("No Connection..."); | |
} | |
} | |
public static void addProduct() | |
{ | |
Connection conn=null; | |
Scanner scr=new Scanner(System.in); | |
System.out.println("enter product id"); | |
int id=scr.nextInt(); | |
System.out.println("Enter Product Name:"); | |
String pname=scr.next(); | |
System.out.println("Enter Product Price:"); | |
double price=scr.nextDouble(); | |
System.out.println("Enter Descriptions:"); | |
String des=scr.next(); | |
String query="INSERT INTO PRODUCTDB VALUES(?,?,?,?)"; | |
PreparedStatement pstm; | |
try { | |
pstm =conn.prepareStatement(query); | |
pstm.setInt(1,id); | |
pstm.setString(2,pname); | |
pstm.setDouble(3,price); | |
pstm.setString(4,des); | |
int status=pstm.executeUpdate(); | |
System.out.println("status"+status); | |
} | |
catch (SQLException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} | |
========================================================================================================= | |
oracle.properties | |
oracle.driver=oracle.jdbc.driver.OracleDriver | |
oracle.url=jdbc:oracle:thin:@localhost:1521:xe | |
oracle.username=system | |
oracle.password=Capgemini123 | |
========================================================================================================= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1)MyTest.java | |
============ | |
package com.cg.DemoTwo.ui; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.util.Properties; | |
import java.util.Scanner; | |
public class MyTest { | |
public static void main(String[] args) | |
{ | |
try { | |
FileInputStream fileRead= | |
new FileInputStream("oracle.properties"); | |
Properties pros =new Properties(); | |
pros.load(fileRead); //Load Properties File | |
String driver=pros.getProperty("oracle.driver"); | |
String url=pros.getProperty("oracle.url"); | |
String uname=pros.getProperty("oracle.username"); | |
String upass=pros.getProperty("oracle.password"); | |
//Load The Driver | |
Class.forName(driver); | |
//Making Connection | |
Connection conn= | |
DriverManager.getConnection(url, uname, upass); | |
System.out.println("Connection Established..."); | |
String query="SELECT prod_id,prod_name,prod_price,prod_des FROM PRODUCTDB"; | |
PreparedStatement pstm=conn.prepareStatement(query); | |
ResultSet res=pstm.executeQuery(); | |
while(res.next()) | |
{ | |
System.out.println(res.getInt("prod_id")); | |
System.out.println(res.getString("prod_name")); | |
System.out.println(res.getDouble("prod_price")); | |
System.out.println(res.getString("prod_des")); | |
} | |
} | |
catch ( ClassNotFoundException | SQLException | IOException e) | |
{ | |
e.printStackTrace(); | |
System.out.println("No Connection..."); | |
} | |
} | |
public static void addProduct() | |
{ | |
Connection conn=null; | |
Scanner scr=new Scanner(System.in); | |
System.out.println("enter product id"); | |
int id=scr.nextInt(); | |
System.out.println("Enter Product Name:"); | |
String pname=scr.next(); | |
System.out.println("Enter Product Price:"); | |
double price=scr.nextDouble(); | |
System.out.println("Enter Descriptions:"); | |
String des=scr.next(); | |
String query="INSERT INTO PRODUCTDB VALUES(?,?,?,?)"; | |
PreparedStatement pstm; | |
try { | |
pstm =conn.prepareStatement(query); | |
pstm.setInt(1,id); | |
pstm.setString(2,pname); | |
pstm.setDouble(3,price); | |
pstm.setString(4,des); | |
int status=pstm.executeUpdate(); | |
System.out.println("status"+status); | |
} | |
catch (SQLException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} | |
===================================================================== | |
2)DbUtill.java | |
============== | |
package com.cg.pms.util; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
import java.util.Properties; | |
import com.cg.pms.exception.ProductException; | |
public class DbUtil | |
{ | |
static Connection conn=null; | |
public static Connection getConnection() throws ProductException | |
{ | |
FileInputStream fileRead; | |
try { | |
fileRead = new FileInputStream("oracle.properties"); | |
Properties pros =new Properties(); | |
pros.load(fileRead); //Load Properties File | |
String driver=pros.getProperty("oracle.driver"); | |
String url=pros.getProperty("oracle.url"); | |
String uname=pros.getProperty("oracle.username"); | |
String upass=pros.getProperty("oracle.password"); | |
//Load The Driver | |
Class.forName(driver); | |
//Making Connection | |
conn=DriverManager.getConnection(url, uname, upass); | |
System.out.println("Connection Established..."); | |
} | |
catch (IOException | ClassNotFoundException | SQLException e) | |
{ | |
e.printStackTrace(); | |
throw new ProductException("Connection Not Established......"); | |
} | |
return conn; | |
} | |
} | |
================================================================================ | |
3)ProductServiceImpl.java | |
=========================== | |
package com.cg.pms.service; | |
import java.util.List; | |
import com.cg.pms.dao.IProductDao; | |
import com.cg.pms.dao.ProductDaoImpl; | |
import com.cg.pms.dto.Product; | |
import com.cg.pms.exception.ProductException; | |
public class ProductServiceImpl implements IProductService | |
{ | |
IProductDao prodDao=new ProductDaoImpl(); | |
@Override | |
public int addProduct(Product prod) throws ProductException | |
{ | |
return prodDao.addProduct(prod); | |
} | |
@Override | |
public List<Product> showall() throws ProductException | |
{ | |
return prodDao.showall(); | |
} | |
@Override | |
public Product searchProduct(int prodID) throws ProductException | |
{ | |
return prodDao.searchProduct(prodID); | |
} | |
@Override | |
public void removeProduct(int prodId) | |
{ | |
} | |
} | |
========================================================================== | |
4)ProductDaoImpl.java | |
===================== | |
package com.cg.pms.dao; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import com.cg.pms.dto.Product; | |
import com.cg.pms.exception.ProductException; | |
import com.cg.pms.util.DbUtil; | |
public class ProductDaoImpl implements IProductDao | |
{ | |
Connection conn=null; | |
PreparedStatement pstm=null; | |
@Override | |
public int addProduct(Product prod) throws ProductException | |
{ | |
conn=DbUtil.getConnection(); | |
int status=0; | |
String query="INSERT INTO PRODUCTDB VALUES(?,?,?,?)"; | |
try{ | |
pstm=conn.prepareStatement(query); | |
pstm.setInt(1,prod.getProductId()); | |
pstm.setString(2,prod.getProductName()); | |
pstm.setDouble(3,prod.getProductPrice()); | |
pstm.setString(4,prod.getProductDes()); | |
status=pstm.executeUpdate(); | |
System.out.println("status"+status); | |
} | |
catch(SQLException e) | |
{ | |
e.printStackTrace(); | |
throw new ProductException("Problem in Insert"); | |
} | |
finally{ | |
try{ | |
pstm.close(); | |
conn.close(); | |
} | |
catch(SQLException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
return status; | |
} | |
@Override | |
public List<Product> showall() throws ProductException | |
{ | |
List<Product>myList=new ArrayList<Product>(); | |
conn=DbUtil.getConnection(); | |
String query="SELECT prod_id,prod_name,prod_price,prod_des FROM PRODUCTDB"; | |
try { | |
pstm=conn.prepareStatement(query); | |
ResultSet res=pstm.executeQuery(); | |
while(res.next()) | |
{ | |
Product pr=new Product(); | |
pr.setProductId(res.getInt("prod_id")); | |
pr.setProductName(res.getString("prod_name")); | |
pr.setProductPrice(res.getDouble("prod_price")); | |
pr.setProductDes(res.getString("prod_des")); | |
myList.add(pr); | |
} | |
} | |
catch (SQLException e) | |
{ | |
e.printStackTrace(); | |
throw new ProductException("Problem in show..."); | |
} | |
finally | |
{ | |
try{ | |
pstm.close(); | |
conn.close(); | |
} | |
catch(SQLException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
return myList; | |
} | |
@Override | |
public Product searchProduct(int prodID) throws ProductException | |
{ | |
Product pSearch=null; | |
try { | |
conn=DbUtil.getConnection(); | |
String querythree="SELECT prod_id,prod_name,prod_price,prod_des FROM PRODUCTDB WHERE prod_id=?"; | |
pstm=conn.prepareStatement(querythree); | |
pstm.setInt(1, prodID); | |
ResultSet resOne=pstm.executeQuery(); | |
while(resOne.next()) | |
{ | |
pSearch=new Product(); | |
pSearch.setProductName(resOne.getString("prod_name")); | |
pSearch.setProductPrice(resOne.getDouble("prod_price")); | |
pSearch.setProductDes(resOne.getString("prod_des")); | |
} | |
} | |
catch (ProductException | SQLException e) | |
{ | |
e.printStackTrace(); | |
throw new ProductException("Problem in Search..."); | |
} | |
finally | |
{ | |
try{ | |
pstm.close(); | |
conn.close(); | |
} | |
catch(SQLException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
return pSearch; | |
} | |
@Override | |
public void removeProduct(int prodId) | |
{ | |
} | |
} | |
========================================================================================== | |
5)IproductDaoImpl.java | |
====================== | |
import java.util.List; | |
import com.cg.pms.dto.Product; | |
import com.cg.pms.exception.ProductException; | |
public interface IProductDao | |
{ | |
public int addProduct(Product prod) throws ProductException ; | |
public List<Product>showall() throws ProductException; | |
public Product searchProduct(int prodID) throws ProductException; | |
public void removeProduct(int prodId); | |
} | |
========================================================================== | |
6)IProductService.java | |
============================= | |
import java.util.List; | |
import com.cg.pms.dto.Product; | |
import com.cg.pms.exception.ProductException; | |
public interface IProductService | |
{ | |
public int addProduct(Product prod) throws ProductException; | |
public List<Product>showall() throws ProductException; | |
public Product searchProduct(int prodID) throws ProductException; | |
public void removeProduct(int prodId) throws ProductException; | |
} | |
=========================================================================== | |
7)ProductException.java | |
======================== | |
public class ProductException extends Exception | |
{ | |
public ProductException() | |
{ | |
super(); | |
} | |
public ProductException(String msg) | |
{ | |
super(msg); | |
} | |
} | |
============================================================================ | |
8)Product.java | |
================== | |
package com.cg.pms.dto; | |
public class Product | |
{ | |
//Attributes | |
private int productId; | |
private String productName; | |
private double productPrice; | |
private String productDes; | |
//Getters And Setters | |
public int getProductId() { | |
return productId; | |
} | |
public void setProductId(int productId) { | |
this.productId = productId; | |
} | |
public String getProductName() { | |
return productName; | |
} | |
public void setProductName(String productName) { | |
this.productName = productName; | |
} | |
public double getProductPrice() { | |
return productPrice; | |
} | |
public void setProductPrice(double productPrice) { | |
this.productPrice = productPrice; | |
} | |
public String getProductDes() { | |
return productDes; | |
} | |
public void setProductDes(String productDes) { | |
this.productDes = productDes; | |
} | |
//Constructors | |
public Product() | |
{ | |
// default Constructor | |
} | |
public Product(int productId, String productName, double productPrice, | |
String productDes) | |
{ | |
super(); | |
this.productId = productId; | |
this.productName = productName; | |
this.productPrice = productPrice; | |
this.productDes = productDes; | |
} | |
@Override | |
public String toString() | |
{ | |
return "Product [productId=" + productId + ", productName=" | |
+ productName + ", productPrice=" + productPrice | |
+ ", productDes=" + productDes + "]"; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment