Guest User

Untitled

a guest
Feb 7th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. package Datatypes1;
  2. import java.io.IOException;
  3. import java.sql.*;
  4. import java.sql.SQLException;
  5. import java.util.Scanner;
  6. public class Database2 {
  7. Scanner input = new Scanner(System.in);
  8. public static void main(String args[]) throws IOException {
  9. char ans;
  10. System.out.println("Welcome tp the coffee shop management system");
  11. Database2 database2 = new Database2();
  12. database2.connect();
  13. database2.createTable();
  14. do{
  15. database2.obtaindata();
  16. System.out.println("Do you want to enter more data:y/n?");
  17. ans =(char) System.in.read();
  18. }while( ans == 'y');
  19.  
  20. database2.disconnect();
  21. }
  22.  
  23. //create the connection
  24. String url="jdbc:postgresql://localhost:5432/dreamhome1";
  25. String user = "postgres";
  26. String pass = "postgres";
  27.  
  28.  
  29. //database variables
  30. Connection con = null;
  31. ResultSet rs = null;
  32. Statement stmt = null;
  33.  
  34.  
  35.  
  36. //connect with the database
  37. public void connect(){
  38. try{
  39. //open connection
  40. con=DriverManager.getConnection(url,user,pass);
  41.  
  42. //show that is it connected
  43. System.out.println("Connection successful");
  44.  
  45. }catch(SQLException e){
  46. e.printStackTrace();
  47. }
  48. }
  49.  
  50. //create table in the database
  51. public void createTable(){
  52. try{
  53. stmt=con.createStatement();
  54. String sql="CREATE TABLE IF NOT EXISTS product (product_id SERIAL PRIMARY KEY NOT NULL,"+
  55. "product_name VARCHAR(100) NOT NULL,"+
  56. "product_price REAL)";
  57. stmt.executeUpdate(sql);
  58. System.out.println("table created successfully!");
  59. stmt.close();
  60. }catch(SQLException e){
  61. e.printStackTrace();
  62.  
  63. }
  64. }
  65.  
  66. public void obtaindata(){
  67. System.out.println("enter the data you want to insert");
  68. System.out.print("Product Name");
  69. String productname=input.nextLine();
  70. System.out.println("Product Price");
  71. Double productprice=input.nextDouble();
  72. Database2 database2=new Database2();
  73. database2.insertRecord(productname,productprice);
  74.  
  75. }
  76.  
  77.  
  78. public void insertRecord(String prodname,Double prodprice){
  79. try{
  80.  
  81. PreparedStatement st=con.prepareStatement("INSERT INTO public.product(product_name,product_price) VALUES (nextval('productid_sequence'),?,?)");
  82. st.setString(1,prodname);
  83. st.setDouble(2,prodprice);
  84. int i =st.executeUpdate();
  85. st.close();
  86. }catch(SQLException e){
  87. e.printStackTrace();
  88. }
  89. }
  90.  
  91. //disconnect with the database
  92. public void disconnect(){
  93. try{
  94. if(con != null){
  95. con.close();
  96. }
  97.  
  98. if(stmt != null){
  99. stmt.close();
  100. }
  101.  
  102. if(rs != null){
  103. rs.close();
  104. }
  105.  
  106. }catch(SQLException e){
  107. e.printStackTrace();
  108. }
  109.  
  110. }
Add Comment
Please, Sign In to add comment