Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Server;
- import java.sql.*;
- public class Connect {
- private ResultSet result;
- private Statement state;
- private Connection conn;
- public Connect() {
- try {
- Class.forName("org.postgresql.Driver");
- String url = "jdbc:postgresql://localhost:5432/Purchases";
- String user = "postgres";
- String password = "";
- //Initialization
- Connection conn = DriverManager.getConnection(url, user, password);
- setConn(DriverManager.getConnection(url, user, password));
- //Creating Statement
- Statement state = conn.createStatement();
- setState(conn.createStatement());
- //The object ResultSet contains the SQL
- ResultSet result = state.executeQuery("SELECT * FROM purchases");
- setResult(state.executeQuery("SELECT * FROM purchases"));
- while(result.next()) {
- //getting all the columns
- System.out.println("Date :"+ result.getDate(1) + "\nprice : "
- + result.getDouble(2) + "\nshop_category: "
- + result.getString(3) + "\nshop_name : "
- + result.getString(4) + "\ncustomer name : "
- + result.getString(5) + "\ncustomer_id : "
- + result.getInt(6));
- }
- } catch(ClassNotFoundException cnf) {
- cnf.printStackTrace();
- } catch(SQLException e) {
- e.printStackTrace();
- }
- }
- public Statement getState() {
- return state;
- }
- public void setState(Statement state) {
- this.state = state;
- }
- public Connection getConn() {
- return conn;
- }
- public void setConn(Connection conn) {
- this.conn = conn;
- }
- public ResultSet getResult() {
- return result;
- }
- public void setResult(ResultSet result) {
- this.result = result;
- }
- }
Add Comment
Please, Sign In to add comment