Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.IOException;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.Properties;
  9. import java.util.Scanner;
  10.  
  11. public class MyTest {
  12.  
  13. public static void main(String[] args)
  14. {
  15.  
  16. try {
  17. FileInputStream fileRead=
  18. new FileInputStream("oracle.properties");
  19. Properties pros =new Properties();
  20. pros.load(fileRead); //Load Properties File
  21.  
  22. String driver=pros.getProperty("oracle.driver");
  23. String url=pros.getProperty("oracle.url");
  24. String uname=pros.getProperty("oracle.username");
  25. String upass=pros.getProperty("oracle.password");
  26.  
  27. //Load The Driver
  28. Class.forName(driver);
  29. //Making Connection
  30. Connection conn=
  31. DriverManager.getConnection(url, uname, upass);
  32. System.out.println("Connection Established...");
  33.  
  34. String query="SELECT prod_id,prod_name,prod_price,prod_des FROM PRODUCTDB";
  35.  
  36. PreparedStatement pstm=conn.prepareStatement(query);
  37. ResultSet res=pstm.executeQuery();
  38. while(res.next())
  39. {
  40. System.out.println(res.getInt("prod_id"));
  41. System.out.println(res.getString("prod_name"));
  42. System.out.println(res.getDouble("prod_price"));
  43. System.out.println(res.getString("prod_des"));
  44. }
  45. }
  46. catch ( ClassNotFoundException | SQLException | IOException e)
  47. {
  48. e.printStackTrace();
  49. System.out.println("No Connection...");
  50. }
  51. }
  52. public static void addProduct()
  53. {
  54. Connection conn=null;
  55. Scanner scr=new Scanner(System.in);
  56. System.out.println("enter product id");
  57. int id=scr.nextInt();
  58. System.out.println("Enter Product Name:");
  59. String pname=scr.next();
  60. System.out.println("Enter Product Price:");
  61. double price=scr.nextDouble();
  62. System.out.println("Enter Descriptions:");
  63. String des=scr.next();
  64.  
  65. String query="INSERT INTO PRODUCTDB VALUES(?,?,?,?)";
  66.  
  67. PreparedStatement pstm;
  68. try {
  69. pstm =conn.prepareStatement(query);
  70. pstm.setInt(1,id);
  71. pstm.setString(2,pname);
  72. pstm.setDouble(3,price);
  73. pstm.setString(4,des);
  74. int status=pstm.executeUpdate();
  75. System.out.println("status"+status);
  76. }
  77. catch (SQLException e)
  78. {
  79. e.printStackTrace();
  80. }
  81.  
  82. }
  83.  
  84. }
  85. =========================================================================================================
  86. oracle.properties
  87. oracle.driver=oracle.jdbc.driver.OracleDriver
  88. oracle.url=jdbc:oracle:thin:@localhost:1521:xe
  89. oracle.username=system
  90. oracle.password=Capgemini123
  91. =========================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement