java用jdbc连接neo4j
环境:
客户端:
window 8.1 eclipse mars
jdk1.7
服务端:
ubuntu14.04
neo4j 2.3.0
带密码
1.新建测试java文件neo4jJdbc.java:
package n1;需要修改为你的ip和用户名以及密码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.neo4j.jdbc.Driver;
//Class.forName("org.neo4j.jdbc.Driver");
//Connect
public class neo4jJdbc {
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
Connection con = DriverManager.getConnection("jdbc:neo4j://localhost:7474/","usrname","password");
//Querying
try(Statement stmt = con.createStatement())
{
ResultSet rs = stmt.executeQuery("MATCH (n) RETURN n");
while(rs.next())
{
System.out.println(rs.getString("n"));
}
}
}
}
2.导入包:
Neo4j-2.0.1-SNAPSHOT Version:http://dist.neo4j.org/neo4j-jdbc/neo4j-jdbc-2.0.1-SNAPSHOT-jar-with-dependencies.jar
会报错:
Embedded Neo4j support not enabled org/neo4j/graphdb/GraphDatabaseService
需要再导入:neo4j 的lib文件夹下的neo4j-kernel-2.3.0-M02.jar
3.往数据库写入文件:
http://localhost:7474/browser/
:play movies
然后运行
查看的时候可以看到图
4.在java下查看,运行1中的java代码 ,运行结果:
Starting the Apache HTTP client
Connecting to URL http://localhost:7474/
Executing query: MATCH (n) RETURN n
with params {}
Starting the Apache HTTP client
{"tagline":"Welcome to the Real World","title":"The Matrix","released":1999}
{"name":"Keanu Reeves","born":1964}
{"name":"Carrie-Anne Moss","born":1967}
{"name":"Laurence Fishburne","born":1961}
{"name":"Hugo Weaving","born":1960}
{"name":"Andy Wachowski","born":1967}
{"name":"Lana Wachowski","born":1965}
{"name":"Joel Silver","born":1952}
{"name":"Emil Eifrem","born":1978}
{"tagline":"Free your mind","title":"The Matrix Reloaded","released":2003}
{"tagline":"Everything that has a beginning has an end","title":"The Matrix Revolutions","released":2003}
{"tagline":"Evil has its winning ways","title":"The Devil's Advocate","released":1997}
{"name":"Charlize Theron","born":1975}
{"name":"Al Pacino","born":1940}
{"name":"Taylor Hackford","born":1944}
{"title":"A Few Good Men","released":1992,"tagline":"In the heart of the nation's capital, in a courthouse of the U.S. government, one man will stop at nothing to keep his honor, and one will stop at nothing to find the truth."}
{"name":"Tom Cruise","born":1962}
{"name":"Jack Nicholson","born":1937}
{"name":"Demi Moore","born":1962}
{"name":"Kevin Bacon","born":1958}
{"name":"Kiefer Sutherland","born":1966}
{"name":"Noah Wyle","born":1971}
{"name":"Cuba Gooding Jr.","born":1968}
{"name":"Kevin Pollak","born":1957}
{"name":"J.T. Walsh","born":1943}
{"name":"James Marshall","born":1967}
{"name":"Christopher Guest","born":1948}
{"name":"Rob Reiner","born":1947}
{"name":"Aaron Sorkin","born":1961}
{"tagline":"I feel the need, the need for speed.","title":"Top Gun","released":1986}
{"name":"Kelly McGillis","born":1957}
{"name":"Val Kilmer","born":1959}
{"name":"Anthony Edwards","born":1962}
{"name":"Tom Skerritt","born":1933}
{"name":"Meg Ryan","born":1961}
{"name":"Tony Scott","born":1944}
{"name":"Jim Cash","born":1941}
{"tagline":"The rest of his life begins now.","title":"Jerry Maguire","released":2000}
{"name":"Renee Zellweger","born":1969}
{"name":"Kelly Preston","born":1962}
{"name":"Jerry O'Connell","born":1974}
{"name":"Jay Mohr","born":1970}
{"name":"Bonnie Hunt","born":1961}
{"name":"Regina King","born":1971}
{"name":"Jonathan Lipnicki","born":1996}
{"name":"Cameron Crowe","born":1957}
{"title":"Stand By Me","released":1986,"tagline":"For some, it's the last real taste of innocence, and the first real taste of life. But for everyone, it's the time that memories are made of."}
{"name":"River Phoenix","born":1970}
{"name":"Corey Feldman","born":1971}
{"name":"Wil Wheaton","born":1972}
{"name":"John Cusack","born":1966}
{"name":"Marshall Bell","born":1942}
{"tagline":"A comedy from the heart that goes for the throat.","title":"As Good as It Gets","released":1997}
{"name":"Helen Hunt","born":1963}
{"name":"Greg Kinnear","born":1963}
{"name":"James L. *s","born":1940}
{"tagline":"After life there is more. The end is just the beginning.","title":"What Dreams May Come","released":1998}
{"name":"Annabella Sciorra","born":1960}
{"name":"Max von Sydow","born":1929}
{"name":"Werner Herzog","born":1942}
{"name":"Robin Williams","born":1951}
{"name":"Vincent Ward","born":1956}
{"tagline":"First loves last. Forever.","title":"Snow Falling on Cedars","released":1999}
{"name":"Ethan Hawke","born":1970}
{"name":"Rick Yune","born":1971}
{"name":"James Cromwell","born":1940}
{"name":"Scott Hicks","born":1953}
{"tagline":"At odds in life... in love on-line.","title":"You've Got Mail","released":1998}
{"name":"Parker Posey","born":1968}
{"name":"Dave Chappelle","born":1973}
{"name":"Steve Zahn","born":1967}
{"name":"Tom Hanks","born":1956}
{"name":"Nora Ephron","born":1941}
{"tagline":"What if someone you never met, someone you never saw, someone you never knew was the only someone for you?","title":"Sleepless in Seattle","released":1993}
{"name":"Rita Wilson","born":1956}
{"name":"Bill Pullman","born":1953}
{"name":"Victor Garber","born":1949}
{"name":"Rosie O'Donnell","born":1962}
{"tagline":"A story of love, lava and burning desire.","title":"Joe Versus the Volcano","released":1990}
{"name":"John Patrick Stanley","born":1950}
{"name":"Nathan Lane","born":1956}
{"tagline":"At odds in life... in love on-line.","title":"When Harry Met Sally","released":1998}
{"name":"Billy Crystal","born":1948}
{"name":"Carrie Fisher","born":1956}
{"name":"Bruno Kirby","born":1949}
{"tagline":"In every life there comes a time when that thing you dream becomes that thing you do","title":"That Thing You Do","released":1996}
{"name":"Liv Tyler","born":1977}
{"tagline":"Pain heals, Chicks dig scars... Glory lasts forever","title":"The Replacements","released":2000}
{"name":"*e Langton","born":1970}
{"name":"Gene Hackman","born":1930}
{"name":"Orlando Jones","born":1968}
{"name":"Howard Deutch","born":1950}
{"title":"RescueDawn","released":2006,"tagline":"Based on the extraordinary true story of one man's fight for freedom"}
{"name":"Christian Bale","born":1974}
{"name":"Zach Grenier","born":1954}
{"tagline":"Come as you are","title":"The Birdcage","released":1996}
{"name":"Mike Nichols","born":1931}
{"title":"Unforgiven","released":1992,"tagline":"It's a hell of a thing, killing a man"}
{"name":"Richard Harris","born":1930}
{"name":"Clint Eastwood","born":1930}
{"tagline":"The hottest data on earth. In the coolest head in town","title":"Johnny Mnemonic","released":1995}
{"name":"Takeshi Kitano","born":1947}
{"name":"Dina Meyer","born":1968}
{"name":"Ice-T","born":1958}
{"name":"Robert Longo","born":1953}
{"tagline":"Everything is connected","title":"Cloud Atlas","released":2012}
{"name":"Halle Berry","born":1966}
{"name":"Jim Broadbent","born":1949}
{"name":"Tom Tykwer","born":1965}
{"tagline":"Break The Codes","title":"The Da Vinci Code","released":2006}
{"name":"Ian McKellen","born":1939}
{"name":"Audrey Tautou","born":1976}
{"name":"Paul Bettany","born":1971}
{"name":"Ron Howard","born":1954}
{"tagline":"Freedom! Forever!","title":"V for Vendetta","released":2006}
{"name":"Natalie Portman","born":1981}
{"name":"Stephen Rea","born":1946}
{"name":"John Hurt","born":1940}
{"name":"Ben Miles","born":1967}
{"tagline":"Speed has no limits","title":"Speed Racer","released":2008}
{"name":"Emile Hirsch","born":1985}
{"name":"John Goodman","born":1960}
{"name":"Susan Sarandon","born":1946}
{"name":"Matthew Fox","born":1966}
{"name":"Christina Ricci","born":1980}
{"name":"Rain","born":1982}
{"title":"Ninja Assassin","released":2009,"tagline":"Prepare to enter a secret world of assassins"}
{"name":"Naomie Harris"}
{"tagline":"Walk a mile you'll never forget.","title":"The Green Mile","released":1999}
{"name":"Michael Clarke Duncan","born":1957}
{"name":"David Morse","born":1953}
{"name":"Sam Rockwell","born":1968}
{"name":"Gary Sinise","born":1955}
{"name":"Patricia Clarkson","born":1959}
{"name":"Frank Darabont","born":1959}
{"title":"Frost/Nixon","released":2008,"tagline":"400 million people were waiting for the truth."}
{"name":"Frank Langella","born":1938}
{"name":"Michael Sheen","born":1969}
{"name":"Oliver Platt","born":1960}
{"title":"Hoffa","released":1992,"tagline":"He didn't want law. He wanted justice."}
{"name":"Danny DeVito","born":1944}
{"name":"John C. Reilly","born":1965}
{"tagline":"Houston, we have a problem.","title":"Apollo 13","released":1995}
{"name":"Ed Harris","born":1950}
{"name":"Bill Paxton","born":1955}
{"tagline":"Don't Breathe. Don't Look Back.","title":"Twister","released":1996}
{"name":"Philip Seymour Hoffman","born":1967}
{"name":"Jan de Bont","born":1943}
{"title":"Cast Away","released":2000,"tagline":"At the edge of the world, his journey begins."}
{"name":"Robert Zemeckis","born":1951}
{"released":1975,"tagline":"If he's crazy, what does that make you?","title":"One Flew Over the Cuckoo's Nest"}
{"name":"Milos Forman","born":1932}
{"title":"Something's Gotta Give","released":2003}
{"name":"Diane Keaton","born":1946}
{"name":"Nancy Meyers","born":1949}
{"tagline":"One robot's 200 year journey to become an ordinary man.","title":"Bicentennial Man","released":1999}
{"name":"Chris Columbus","born":1958}
{"tagline":"A stiff drink. A little mascara. A lot of nerve. Who said they couldn't bring down the Soviet empire.","title":"Charlie Wilson's War","released":2007}
{"name":"Julia Roberts","born":1967}
{"tagline":"This Holiday Season鈥?Believe","title":"The Polar Express","released":2004}
{"tagline":"Once in a lifetime you get a chance to do something different.","title":"A League of Their Own","released":1992}
{"name":"Madonna","born":1954}
{"name":"Geena Davis","born":1956}
{"name":"Lori Petty","born":1963}
{"name":"Penny Marshall","born":1943}
{"name":"Paul Blythe"}
{"name":"Angela Scope"}
{"name":"Jessica Thompson"}
{"name":"James Thompson"}
Unexpected token END_ARRAY
errors-next-token = FIELD_NAME
null
5.创建一个图:
package test1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.neo4j.jdbc.Driver;
//Class.forName("org.neo4j.jdbc.Driver");
//Connect
public class neo4jJdbc {
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
Connection con = DriverManager.getConnection("jdbc:neo4j://219.219.220.116:7474/","neo4j","184185");
//Querying
try(Statement stmt = con.createStatement())
{
ResultSet rs = stmt.executeQuery("create (n0:Person { name: 'S' }),(n1:Person { name: 'A1' }),(n2:Person { name: 'A2' }),(n3:Person { name: 'A3' }),(n4:Person { name: 'A4' }),(n5:Person { name: 'A5' }),(m1:Person { name: 'B1' }),(m2:Person { name: 'B2' }),(m3:Person { name: 'B3' }),(m0:Person { name: 'D' }),(n0)-[:KNOWS]->(n1),(n1)-[:KNOWS]->(n2),(n2)-[:KNOWS]->(n3),(n3)-[:KNOWS]->(n4),(n4)-[:KNOWS]->(n5),(n5)-[:KNOWS]->(m0),(n0)-[:KNOWS]->(m1),(m1)-[:KNOWS]->(m2),(m2)-[:KNOWS]->(m3),(m3)-[:KNOWS]->(m0)");
while(rs.next())
{
System.out.println(rs.getString("n"));
}
}
}
}
运行结果:
Starting the Apache HTTP client
Connecting to URL http://219.219.220.116:7474/
Executing query: create (n0:Person { name: 'S' }),(n1:Person { name: 'A1' }),(n2:Person { name: 'A2' }),(n3:Person { name: 'A3' }),(n4:Person { name: 'A4' }),(n5:Person { name: 'A5' }),(m1:Person { name: 'B1' }),(m2:Person { name: 'B2' }),(m3:Person { name: 'B3' }),(m0:Person { name: 'D' }),(n0)-[:KNOWS]->(n1),(n1)-[:KNOWS]->(n2),(n2)-[:KNOWS]->(n3),(n3)-[:KNOWS]->(n4),(n4)-[:KNOWS]->(n5),(n5)-[:KNOWS]->(m0),(n0)-[:KNOWS]->(m1),(m1)-[:KNOWS]->(m2),(m2)-[:KNOWS]->(m3),(m3)-[:KNOWS]->(m0)
with params {}
Starting the Apache HTTP client
Unexpected token END_ARRAY
errors-next-token = FIELD_NAME
null
在http://localhost:7474/browser/查看:
先删除原来的图:
match n optional match (n)-[r]->() delete n,r
查看:
match n return n
参考:
【1】https://github.com/rickardoberg/neo4j-jdbc