Reputation: 541
Hi,
I've created a maven project in eclipse and added the dependency:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc</artifactId>
<version>3.0</version>
</dependency>
I'm just trying to get a connection to my db working so that I can move on to integrate the connection with my main project but I'm having trouble getting things off the ground.
I've used the code example given on the official repo:
import org.neo4j.jdbc.Connection;
import org.neo4j.jdbc.PreparedStatement;
import org.neo4j.jdbc.ResultSet;
public class Neo4jConnectionTest {
public static void main(String[] args) {
// Connect
Connection con = DriverManager.getConnection(
"jdbc:neo4j:bolt://localhost");
// Querying
String query = "MATCH (u:User)-[:FRIEND]-(f:User)
WHERE u.name = {1}
RETURN f.name, f.age";
try {
PreparedStatement stmt = con.prepareStatement(query);
stmt.setString(1,"John");
ResultSet rs = con.execute();
while (rs.next()) {
System.out.println(
"Friend: "+rs.getString("f.name")+" is "+rs.getInt("f.age"));
}
} catch (Exception e) { e.printStackTrace(); }
con.close();
}
}
I am unable to compile this as:
DriverManager
cannot be resolved within the neo4j-jdbc-3.0,
Prepared stmt = con.prepareStatement(query);
causes a type mismatch,
and con.execute()
is undefined for type org.neo4j.jdbc.Connection
I'd greatly appreciate any advice and expertise on the matter, thanks.
Upvotes: 2
Views: 4182
Reputation: 11735
DriverManager
, Connection
, PreparedStatement
and ResultSet
are all classes or interfaces from the java.sql
package, which is part of the JDK. I guess the documentation assumes you'll use an IDE which will find the correct import for you.
It's the point of using the JDBC driver: you use the JDBC API, not a proprietary one (i.e. in a vendor package).
Update
There was a typo in the neo4j-jdbc readme, it should have read
ResultSet rs = stmt.execute();
instead of
ResultSet rs = con.execute();
otherwise the PreparedStatement
has not use (and the code doesn't compile because there's no no-arg overload of Connection.execute()
).
Update 2
The documented dependency was also wrong, as neo4j-jdbc
does not contain any driver.
You should depend on:
either the all-in-one module, which gives you the opportunity to use the Bolt or HTTP protocols:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-driver</artifactId>
<version>3.0</version>
</dependency>
or the module for a specific protocol:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-bolt</artifactId>
<version>3.0</version>
</dependency>
<!-- or -->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-http</artifactId>
<version>3.0</version>
</dependency>
Update 3
Both problems have now been fixed in the documentation of the project.
Upvotes: 1
Reputation: 541
By changing to:
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>1.0.4</version>
</dependency>
I was able to successfully connect to the Neo4j 3.0 server using an example from Neo4j's site:
public static void main(String[] args) {
Driver driver = GraphDatabase.driver(
"bolt://localhost", AuthTokens.basic( "neo4j", "neo4j" ) );
Session session = driver.session();
session.run( "CREATE (a:Person {name:'Arthur', title:'King'})" );
StatementResult result = session.run(
"MATCH (a:Person)
WHERE a.name = 'Arthur'
RETURN a.name AS name, a.title AS title");
while ( result.hasNext() ) {
Record record = result.next();
System.out.println( record.get( "title" ).asString() +
" " + record.get("name").asString() );
}
session.close();
driver.close();
}
I thought I'd share as this worked instantly with no hassle.
Upvotes: 2