Neo4j类型不匹配

时间:2018-10-09 14:57:01

标签: java eclipse jdbc neo4j

我试图在eclipse中编写连接到neo4j的JDBC代码。我可以将其连接到数据库,但是在运行查询时遇到了麻烦。我已导入Neo4J驱动程序jar以及所需的大多数驱动程序。话虽如此,我仍然在两个地方出现错误:

PreparedStatement statement = conn.prepareStatement(cypher);

给我错误:

此行有多个标记

- The method prepareStatement(String) is undefined for the type Connection

- Type mismatch: cannot convert from java.sql.PreparedStatement to org.neo4j.jdbc.PreparedStatement

我很困惑为什么要包含java.sql,因为我没有将它导入到此类中。

我的其他错误在第

连接conn = DriverManager.getConnection(“ jdbc:neo4j:bolt:// localhost”,“”,“”);

我在这里得到的错误是:无法解决DriverManager。

我的代码(未完成)如下。我的问题是,我应该怎么做才能解决这些错误。

private static void runCypher(Connection conn, String cypher) {
    PreparedStatement statement = conn.prepareStatement(cypher);


}

public static void main (String[] args) {
    try {   
        Connection conn = DriverManager.getConnection("jdbc:neo4j:bolt://localhost", "", "");

        String cypher = "";
        String option = "";
        String instr = "Enter a: Show distinct sids and snames of suppliers who supply a red part or a green part." + "\n"
                + "Enter b: Show distinct sids and snames of suppliers who supply a red part and a green part."
                + "\n" + "Enter c: Insert a new supplier." + "\n"
                + "Enter d:  Quit Program.";

    while (true) {
        option = JOptionPane.showInputDialog(instr);
        if (option.equals("a")) {
            cypher = "match (s:Suppliers)-[:supplies]->(p:Parts) where p.color = ‘red’ or p.color = ‘green’ return distinct s.sid, s.sname;";
            runCypher(conn, cypher);
        } else if (option.equalsIgnoreCase("b")) {
            cypher = "match (p1:Parts)<-[:supplies]-(s:Suppliers)-[:supplies]->(p2:Parts) where p1.color = ‘red’ and p2.color = ‘green’ return distinct s.sid, s.sname;";
            runCypher(conn, cypher);
        } else if (option.equals("c")) {
            cypher = "match (p:Parts)<-[sup:supplies]-(s:Suppliers) return p.pid, p.pname, max(toInteger(sup.cost)) order by p.pid;";
            runCypher(conn, cypher);
        } else {
            break;
        }


    }}catch(Exception e ) {
        System.out.println("Program terminates due to errors");
        e.printStackTrace(); // for debugging
    }
}

我的进口商品:

import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.jdbc.PreparedStatement;
import org.neo4j.jdbc.ResultSet;
import org.neo4j.jdbc.ResultSetMetaData;

1 个答案:

答案 0 :(得分:1)

错误消息DriverManager cannot be resolved表示编译器不知道该类/字段/ ...,因此它可能缺少相应的导入:

import java.sql.DriverManager;

错误The method prepareStatement(String) is undefined for the type Connection表示未找到此方法,因为您正在导入并正在使用错误的Connection类。我不记得曾经使用过驱动程序JAR中的类,尽管需要加载驱动程序本身(前一段时间...)。基于给DriverManager的URL,正确的类用于Java。您只需要使用java.sql包中的类即可。...

import java.sql.Connection;
import java.sql.PreparedStatement;
...

这是一个非常简单的示例:

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Neo4j {

    public static void main(String[] args) throws Exception {
        try (Connection conn = DriverManager.getConnection("jdbc:neo4j:bolt://localhost", "neo4j", "test123")) {
            PreparedStatement stmt = conn.prepareStatement("MATCH (a:Aplz) RETURN *");
            ResultSet rset = stmt.executeQuery();
            while (rset.next()) {
                System.out.println(rset.getObject(1));
            }
        }
    }
}

实际上,从不使用Java,您只需要DriverManager,编译器就可以做到这一点:

import java.sql.DriverManager;

public class Neo4j {

    public static void main(String[] args) throws Exception {
        try (var conn = DriverManager.getConnection("jdbc:neo4j:bolt://localhost", "neo4j", "test123")) {
            var stmt = conn.prepareStatement("MATCH (a:Aplz) RETURN *");
            var rset = stmt.executeQuery();
            while (rset.next()) {
                System.out.println(rset.getObject(1));
            }
        }
    }

}