Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- =========================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement