Guest User

Untitled

a guest
Nov 6th, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package com.swalahamani.ts.test;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. /**
  17. *
  18. * @author swalahamani
  19. */
  20. public class JDBC_Ex {
  21. private final String DB_URL = "jdbc:postgresql://localhost:5432/DB_NAME";
  22. private final String DB_USR = "postgres";
  23. private final String DB_PAS = "password";
  24.  
  25.  
  26. /*
  27. sql qyert -> SELECT * FROM DAILY_EXPENSE WHERE AMOUNT = 45;
  28.  
  29. showDBTable("DAILY_EXPENSE", "AMOUNT", "45");
  30.  
  31. */
  32. public void showDBTable(String tableName, String colName, String value) {
  33. Connection con;
  34. Statement smt;
  35. ResultSet rs;
  36.  
  37. try {
  38. Class.forName("org.postgresql.Driver");
  39.  
  40. con = DriverManager.getConnection(DB_URL, DB_USR, DB_PAS);
  41.  
  42. smt = con.createStatement();
  43.  
  44. rs = smt.executeQuery("SELECT * FROM "+tableName+" WHERE "+colName+" = "+value);
  45.  
  46. while(rs.next()) {
  47. System.out.println(rs.getString("DATEE"));
  48. System.out.println(rs.getString("NAME"));
  49. System.out.println(rs.getString("DESCRIPTION"));
  50. System.out.println(rs.getString("AMOUNT"));
  51. }
  52.  
  53. rs.close();
  54. smt.close();
  55. con.close();
  56.  
  57.  
  58.  
  59. } catch (ClassNotFoundException ex) {
  60. Logger.getLogger(JDBC_Ex.class.getName()).log(Level.SEVERE, null, ex);
  61. } catch (SQLException ex) {
  62. Logger.getLogger(JDBC_Ex.class.getName()).log(Level.SEVERE, null, ex);
  63. }
  64.  
  65. }
  66.  
  67.  
  68. }
Add Comment
Please, Sign In to add comment