Wednesday, 30 November 2016

REST API WITH POSTGRES DATABASE

      REST API WITH POSTGRES DATABASE



  1. First i started working on eclipse mars ,but i got lot of errors even at sample program that given in https://www.tutorialspoint.com/restful/restful_first_application.htm. Main drop back of eclipse is we need to add many jersey files and jdk 1.7 , tomcat into application
  2. Then i move on to netbeans because i contain in bulid jersey files, jdk , tomcat.
Netbeans setup Environment


Give Executable permission :  $ chmod +x ~/Downloads/netbeans-8.1*.sh

Start Installer : $ cd ~/Downloads &&   ./netbeans-8.1*.sh

Set JDK location      : Browse to JDK location
-----------------------------------------------------------------------
Tomcat server setup Environment

  http://tomcat.apache.org/download-80.cgi => Find Full Documentation => .tar.gz => Download
$ tar –zxvf apache-tomcat-8.5.5.tar.gz
$ cd apache-tomcat-8.5.5/bin/
To start tomcat server:
$ ./startup.sh
Browse Tomcat running or not:
Localhost:8080
To stop tomcat server:
$ ./shutdown.sh
To change tomcat users name and password :
$ gedit /path/to/apache-tomcat-8.5.5/conf/tomcat-users.xml
To change server port number:
$ gedit /path/to/apache-tomcat-8.5.5/conf/server.xml
------------------------------------------------------------------------------------------------------------------------
Create Rest Api application
  File=>New Project=>Java Web=>Web Application=>Next => Enter Project Name => next=>Select Server (Either GlassFish/Tomcat) => next=>Finish.






-----------------------------------------------------------------------
  

Postgres setup Environment
         
1. sudo apt-get update
2. sudo apt-get install postgresql postgresql-contrib
3. Create a user account called postgres which is associated to postgres role
       sudo -i -u postgres
4. Postgre is prompt by immediately  typing
       psql
5. For Exit Postgres type
       \q
6. For enter into Postgres use this keyword
       sudo -u postgres psql postgres
7. For creating database password
       \password postgres
8. Create database in Postgres
       create database databasename
9. For connecting database
         \c databasename
10. For disconnecting database
          \q


----------------------------------------------------------------------------------------------------






SETTING PASSWORD FOR POSTGRES:

supranimbus04@supranimbus04-Inspiron-3250:~$ sudo -u postgres psql
Password:


It will ask password , If the configuration  of file                                                           $ sudo gedit /etc/postgresql/9.3/main/pg_hba.conf



local   all             postgres                                md5

If we don’t need to password , then change configuration of the
$ sudo gedit/etc/postgresql/9.3/main/pg_hba.conf
local   all             postgres                                peer


If any changes made into $ sudo gedit/etc/postgresql/9.3/main/pg_hba.conf file , Then you need to restart to the service


sudo service postgresql restart

--------------------------------------------------------------------------------------------------------


To view postgres status
       
     $ sudo service postgresql status
--------------------------------------------------------------------------------------------------------



Creating table in postgres

  1. Create table tablename ();
  postgres=# create table testtbl(id int primary key not null,name  char(30));
  
    2. To view list of tables
      
  • \d - is to view list of new tables that are created
  • \dt - is to view list of tables that are present in database

   3. To delete table
         
        Drop table tablename;


   ------------------------------------------------------------------------------------------------------

For JDBC connection
 
  1. Now add this driver into netbeans libraries
  2. For reference visit this link
      http://java2db.com/java-integrations/jdbc-with-postgresql-using-myeclipse-and-netbeans
 
--------------------------------------------------------------------------------------------------------

Employee details program using Rest api with postgres database


 Access.java

Screenshot from 2016-10-21 09:16:44.png

DataBase.java

Screenshot from 2016-10-21 09:18:08.png

Employee.java

Screenshot from 2016-10-21 09:22:28.png
AccessManager.java

Screenshot from 2016-10-21 09:23:46.png



EmployeeSevice.java

Screenshot from 2016-10-21 09:26:02.png

Output
         JSON file will be displayed under this URL
Screenshot from 2016-10-21 09:29:53.png
















No comments:

Post a Comment