How can i create server side application for postgreSQL (for android app and no Firebase) - java

I'm a beginner at java and i'm developing a big app the big issue is that i need a big database for which i should make a server side application(which i don't know how) to connect to Postgres. I did not find anything about making it and connecting it to PostgreSQL to Android Studio for instance.. I'm really confused and can't find anything about the topic. Excuse my bad English.

I recomend to see this example https://spring.io/guides/gs/accessing-data-mysql/
Mysql is being used, but you could simple change the "jdbc driver" to connect with PostgreSQL
First change driver in pom.xml
this
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
to
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
and then replace the url in src/main/resources/application.properties with the connection info of your postgres instance.
spring.datasource.url=jdbc:postgresql://host:port/database
also add this line to the properties:
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
See https://github.com/spring-projects/spring-boot/issues/12007
On postgres user is a reserved keyword, so you have to change the name of the table that maps with User entity:
import javax.persistence.Table;
#Entity // This tells Hibernate to make a table out of this class
#Table(name = "users")
In this way you have the example working.
If not needed to do in Java, you can do it on other languages and frameworks, but the main idea would be to have an API and fetch or post data from/to.
I leave you some useful links to have more information
JDBC https://en.wikipedia.org/wiki/Java_Database_Connectivity
PostgreSQL JDBC Driver : https://github.com/pgjdbc/pgjdbc
ORM: https://en.wikipedia.org/wiki/Object-relational_mapping
Hibernate, a Java ORM http://hibernate.org/orm/
REST: https://spring.io/understanding/REST

Related

Spring Boot Test using Neo4j embedded database with JDBC Bolt driver

Is it possible to connect to an embedded Neo4j database the same way you would do with an H2 in-memory database to mock an Oracle database?
I've tried to do this:
final BoltConnector boltConnector = new BoltConnector("bolt");
graphDb = new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(DB_PATH)
.setConfig(boltConnector.type, BOLT.name())
.setConfig(boltConnector.enabled, TRUE)
.setConfig(boltConnector.listen_address, listenAddress("127.0.0.1", 7688))
.setConfig(boltConnector.encryption_level, DISABLED.name())
.setConfig(GraphDatabaseSettings.auth_enabled, FALSE)
.newGraphDatabase();
And then make a request using the JDBC Bolt driver with the following spring.datasource configuration:
spring:
profiles: test
datasource:
driver-class-name: org.neo4j.jdbc.bolt.BoltDriver
url: jdbc:neo4j:bolt://127.0.0.1:7688/?nossl
But I always get the following error:
Unable to connect to 127.0.0.1:7688, ensure the database is running and that there is a working network connection to it.
Of course the embedded database works when I use the graphDb instance and execute requests against it. But I want my application to connect to the embedded database as it does when connecting to a remote Neo4j database.
This is for testing purpose.
I finally RTFM...
I had the following dependency in my pom.xml:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>3.4.0</version>
</dependency>
Then I found this: https://neo4j.com/docs/java-reference/current/tutorials-java-embedded/#tutorials-java-embedded-bolt
The documentation is a bit outdated because it uses deprecated configuration. But they explain this:
The Neo4j Browser and the official Neo4j Drivers use the Bolt database
protocol to communicate with Neo4j. By default, Neo4j Embedded does
not expose a Bolt connector, but you can enable one. Doing so allows
you to connect the services Neo4j Browser to your embedded instance.
And they make clear the correct dependency to use is:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-bolt</artifactId>
<version>3.4.0</version>
</dependency>

Migrate standard DAO with SQL to Hibernate and/or Spring data JPA [closed]

I'm working on project, that uses Spring framework (Spring MVC, DAO etc.)
Project is not so big and uses Jetty servlet container, PostgreSQL database with standard DAO classes(sql requests). I need to make it possible to use Hibernate/Spring JPA instead SQL queries.
I found a few states describes migration to spring boot:
https://www.baeldung.com/spring-boot-migration
https://3ldn.nl/2016/02/16/spring-boot-in-an-existing-application-part-1/
I'm add spring boot dependencies in project:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.0.9.RELEASE</version>
Also I add package "entity" on the same level with old UserDao class and I need to use Spring JPA Repositories instead of using old DAO classes.
What actions I need to do for that replacement for table Users, that contains user id, name and email?
Need I use Hibernate or Spring JPA is enough?
You don't need to migrate to Spring Boot if you don't want to. Spring Boot has much easier configuration than legacy spring project, but still you can use legacy.
If you want to use Repositories as your data access layer, you can use Spring Data JPA with Postgres Driver Spring Data JPA Docs, but if you want to use only Hibernate with standard DAO classes, and EntityManager, you can add only hibernate dependency, Spring ORM and Postgres Driver. After that you just need to configure your Persistence Unit and Transaction managment withis your application (through persistence.xml or Java configuration).

How to implement ref cursors within stored procedure in MariaDB?

We are currently using MariaDB v10.1.19 to as a database. We wanted to know if this particular version supports the refcursor concept within store procedure like Oracle DB.
Tried to look at some of the sites but could not get a final answer.
We would be using Java8 using below jdbc connector
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.0.1</version>
</dependency>

Neo4j Non-Rest Environment

I have tried running SDN 4, but still can't seem to make it run without its REST service (web service). I wonder if it is possible to make Neo4j run without REST(Web) service? Currently trying to make an app to do simple CRUD to the database like MyBatis.
SDN 4.1 (4.1.1.RELEASE) supports Neo4j running in embedded mode.
You need the embedded driver dependency:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-embedded-driver</artifactId>
<version>2.0.1</version>
</dependency>
and then you'll configure it. Both auto configuration and java configuration modes are supported. The manual contains information about how to do this: http://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#_configuring_the_embedded_driver
There's a sample application that uses the embedded driver: https://github.com/neo4j-examples/sdn4-university/tree/4.1-embedded

What mysql driver do I use with spring/hibernate?

Little confused, is 'driverclassname' and 'hibernate.dialect' both referring to the mysql driver?
What should I be using? Is the connectorJ the one I should use?
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:mysql://localhost/blah"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
I'm using Maven so if I can get the driver from maven that would be ideal.
Running my app in tomcat I get the error:
Cannot create JDBC driver of class 'org.hsqldb.jdbcDriver' for connect URL
Little confused, is 'driverclassname' and 'hibernate.dialect' both referring to the mysql driver?
No, they are not. The driverclassname is referring to, well, the driver class name which is the class from a given JDBC driver that implements java.sql.Driver. The driver class name is driver specific.
When using MySQL's JDBC driver aka MySQL Connector/J, this class is com.mysql.jdbc.Driver as explained in the MySQL Connector/J documentation:
20.3.4.1. Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J
The name of the class that implements
java.sql.Driver in MySQL Connector/J
is com.mysql.jdbc.Driver. (...)
And actually, they even provide instructions to use their driver with Spring. See the section 20.3.5.2.4. Using Connector/J with Spring.
The hibernate.dialect is different, this configuration property is used to define the classname of a Hibernate org.hibernate.dialect.Dialect which allows Hibernate to generate SQL optimized for a particular relational database. Again this is explained in the Hibernate documentation:
3.4. Optional configuration properties
(...) The classname of a Hibernate
org.hibernate.dialect.Dialect which
allows Hibernate to generate SQL
optimized for a particular relational
database.
e.g. full.classname.of.Dialect
In most cases Hibernate will actually
be able to choose the correct
org.hibernate.dialect.Dialect
implementation based on the JDBC
metadata returned by the JDBC driver.
For MySQL 5.x, you should use org.hibernate.dialect.MySQL5InnoDBDialect if you are using InnoDB tables (this would be my recommendation) or org.hibernate.dialect.MySQL5Dialect if you're not. See the section 3.4.1. SQL Dialects for a (non exhaustive) list.
Last point, the Maven part that you didn't even mention in your question... The MySQL JDBC driver is available in the Maven central repository and you should use a repository search engine (as I already suggested). For example, the following query:
http://www.jarvana.com/jarvana/search?search_type=project&project=mysql
allows to find the maven coordinates of the ultimate version in two clicks:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.13</version>
</dependency>
PS: I don't mean to be rude and I'm glad to help but you should really try to leverage the documentation of the products or frameworks you're using. What you're asking in this question is well documented (as I showed) and can be found easily. Learning to find basic information by yourself is a fundamental skill for a software developer in my opinion.
regarding maven mysql definition, here's one that seems to work.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.12</version>
</dependency>
The driverClassName should be referring to the class name of the JDBC driver you'd like to load (as you usually would do using Class#forName() in "plain" JDBC). The one you're currently specifying is correct for the MySQL JDBC driver (Update: you quickly edited it into a HSQLDB JDBC driver one, this is only correct for a Hypersonic DB, not for a MySQL DB).
The hibernate.dialect should be referring to the class name of the Hibernate Dialect implementation you'd like to use for the particular database, so that Hibernate knows what the DB in question understands so that it can autogenerate the suitable SQL statements. The one you're currently specifying is correct for the MySQL database.
That said, it sounds like that you're having problems with it. Probably you haven't installed the MySQL JDBC driver? Are you getting a ClassNotFoundException on it? Just downloading Connector/J, extracting the zip and placing the JAR file in the runtime classpath ought to be sufficient.

Resources