我刚开始玩Crate(版本crate-0.55.2)。 我想通过JDBC连接到Crate。 这是我的示例java代码:
if (!string.IsNullOrEmpty(Request.QueryString["Lang"]))
{
HttpCookie myCookie = (HttpCookie)Request.Cookies["cLang"];
if (myCookie != null)
{
if (myCookie["Lang"] != Request.QueryString["Lang"].ToString())
{
myCookie["Lang"] = Request.QueryString["Lang"].ToString();
myCookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(myCookie);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Request.QueryString["Lang"]);
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(Request.QueryString["Lang"]);
Response.Redirect(Request.Url.AbsoluteUri);
}
}
else
{
string strLang = Request.QueryString["Lang"];
myCookie = new HttpCookie("cLang");
myCookie.Values.Add("Lang", strLang);
myCookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(myCookie);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(strLang);
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(strLang);
}
}
我已经添加了库" crate-jdbc-standalone-1.9.3.jar"到我的Java Build Path。我已经改变了" transport.tcp.port" " crate.yml"中的4301文件。 (所有其他配置都没有改变,只有这个端口)。 当我运行我的Java代码时,我在Eclipse中获得了以下输出:
import java.sql.*;
//import io.crate.*;
public class CrateTestDemo {
private Connection connection;
public static void main(String[] args) {
CrateTestDemo t = new CrateTestDemo();
try {
t.setupDatabase();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
System.out.println("e="+e.getMessage());
}
}
public void setupDatabase() throws ClassNotFoundException, SQLException {
Class.forName("io.crate.client.jdbc.CrateDriver");
connection = DriverManager.getConnection("crate://127.0.0.1:4301");
}
}
在" crate.log"文件我得到了这个:
log4j:WARN No appenders could be found for logger (io.crate.shade.org.elasticsearch.common.compress.lzf).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
java.sql.SQLException: Connect to '127.0.0.1:4301' failed
at io.crate.client.jdbc.CrateConnection.connect(CrateConnection.java:58)
at io.crate.client.jdbc.CrateDriver.connect(CrateDriver.java:67)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at CrateTestDemo.setupDatabase(CrateTestDemo.java:19)
at CrateTestDemo.main(CrateTestDemo.java:10)
Caused by: io.crate.shade.org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []
at io.crate.shade.org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:309)
at io.crate.shade.org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:201)
at io.crate.client.InternalCrateClient.execute(InternalCrateClient.java:87)
at io.crate.client.InternalCrateClient.execute(InternalCrateClient.java:70)
at io.crate.client.InternalCrateClient.sql(InternalCrateClient.java:59)
at io.crate.client.CrateClient.sql(CrateClient.java:152)
at io.crate.client.jdbc.CrateStatement.execute(CrateStatement.java:65)
at io.crate.client.jdbc.CrateStatementBase.executeQuery(CrateStatementBase.java:37)
at io.crate.client.jdbc.CrateDatabaseMetaData.getDatabaseProductVersion(CrateDatabaseMetaData.java:79)
at io.crate.client.jdbc.CrateConnection.connect(CrateConnection.java:56)
... 5 more
e=Connect to '127.0.0.1:4301' failed
有人知道为了让我的小Java代码运行必须做些什么吗?感谢。
答案 0 :(得分:1)
嗯..好吧..我应该在检查兼容性之前:)
我已将其更改为较新的库" crate-jdbc-standalone-1.13.0.jar",现在可以正常使用。
另见:https://github.com/crate/crate/issues/3786 和https://github.com/crate/crate-jdbc/tree/1.13#compatibility