gpt4 book ai didi

java - Neo4j 类型不匹配

转载 作者:行者123 更新时间:2023-12-02 10:44:01 25 4
gpt4 key购买 nike

我正在尝试在 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;

最佳答案

错误消息DriverManager无法解析意味着编译器不知道该类/字段/...因此它可能缺少相应的导入:

import java.sql.DriverManager;

错误方法prepareStatement(String)对于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));
}
}
}

}

关于java - Neo4j 类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52724027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com