Guest User

Untitled

a guest
May 12th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. package Server;
  2.  
  3. import java.sql.*;
  4.  
  5.  
  6. public class Connect {
  7.  
  8. private ResultSet result;
  9. private Statement state;
  10. private Connection conn;
  11.  
  12. public Connect() {
  13. try {
  14. Class.forName("org.postgresql.Driver");
  15.  
  16. String url = "jdbc:postgresql://localhost:5432/Purchases";
  17. String user = "postgres";
  18. String password = "";
  19.  
  20. //Initialization
  21. Connection conn = DriverManager.getConnection(url, user, password);
  22. setConn(DriverManager.getConnection(url, user, password));
  23.  
  24. //Creating Statement
  25. Statement state = conn.createStatement();
  26. setState(conn.createStatement());
  27.  
  28. //The object ResultSet contains the SQL
  29. ResultSet result = state.executeQuery("SELECT * FROM purchases");
  30. setResult(state.executeQuery("SELECT * FROM purchases"));
  31.  
  32. while(result.next()) {
  33.  
  34. //getting all the columns
  35. System.out.println("Date :"+ result.getDate(1) + "\nprice : "
  36. + result.getDouble(2) + "\nshop_category: "
  37. + result.getString(3) + "\nshop_name : "
  38. + result.getString(4) + "\ncustomer name : "
  39. + result.getString(5) + "\ncustomer_id : "
  40. + result.getInt(6));
  41. }
  42.  
  43. } catch(ClassNotFoundException cnf) {
  44. cnf.printStackTrace();
  45.  
  46. } catch(SQLException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50.  
  51. public Statement getState() {
  52. return state;
  53. }
  54.  
  55. public void setState(Statement state) {
  56. this.state = state;
  57. }
  58.  
  59. public Connection getConn() {
  60. return conn;
  61. }
  62.  
  63. public void setConn(Connection conn) {
  64. this.conn = conn;
  65. }
  66.  
  67. public ResultSet getResult() {
  68. return result;
  69. }
  70.  
  71. public void setResult(ResultSet result) {
  72. this.result = result;
  73. }
  74. }
Add Comment
Please, Sign In to add comment