无法在Eclipse中连接到Derby [英] Can't connect to Derby in Eclipse
问题描述
我试图用eclipse开发一个使用derby数据库并在tomcat上运行的web应用程序。
I am trying to develop a web app with eclipse that uses a derby database and runs on tomcat.
我的问题是我无法使用eclipse启动derby服务器(它工作良好的CMD),我不能得到我的servlet建立与数据库的连接,每次我尝试我得到错误:
My problem is that I cannot start the derby server with eclipse (it works fine out of CMD) and I cannot get my servlet to establish a connection with the database, each time I try I get the error:
java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.jdbc.ClientDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.Jieren.servlets.Authenticator.testCredentials(Authenticator.java:84)
at com.Jieren.servlets.Authenticator.doPost(Authenticator.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
我没有任何xml文件, web.xml和这样的管理连接),但从我看到的连接应该是可能通过直接的java代码(这似乎更容易学习与我是相当新的)。
I do not have any xml files that do anything with the connection (I have seen web.xml and such that manage connections) but from what I have seen a connection should be possible via straight java code (which seemed easier to learn with as I am fairly new).
我用来连接的代码如下。
The code that I use to connect with is as follows.
Connection conn = null;
PreparedStatement prestat = null;
ResultSet pw = null;
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection("jdbc:derby://localhost:1527/C:/apache-tomcat-7.0.19/Databases/Jieren;" +
"user=Access;" +
"password=Entry");
prestat = conn.prepareStatement("SELECT password FROM logs WHERE username = ?");
prestat.setString(1, username);
pw = prestat.executeQuery();
if (password.equals(pw.toString())) answer = 1;
pw.close();
pw = null;
prestat.close();
prestat = null;
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
finally{
if (pw != null){
try { pw.close();} catch (SQLException e){;}
pw = null;
}
if (prestat != null){
try { prestat.close();} catch (SQLException e){;}
prestat = null;
}
if (conn != null){
try {conn.close();} catch(SQLException e) {;}
conn = null;
}
}
代码应该工作,如果一切正确配置。通过ij外部Eclipse工作连接到数据库,所以我有一种感觉,有一个设置或一些东西,我需要在eclipse中连接这个。
From what I have figured out from looking around, the code should work if everything else is configured correctly. connecting to the database via ij outside eclipse works, so I have a feeling that there is a setting or something that I need to write in eclipse to connect this.
推荐答案
例外情况是告诉你网络服务器没有运行。当您的连接URL启动jdbc:derby:// hostname时,您告诉Derby您希望以客户端 - 服务器模式运行,这意味着您的客户端应用程序将建立到网络服务器的TCP / IP连接。有关如何设置和操作网络服务器,请参阅此文档: http://db.apache .org / derby / docs / 10.8 / adminguide /
The exception is telling you that your network server is not running. When your connection URL starts jdbc:derby://hostname, then you are telling Derby you wish to run in client-server mode, meaning that your client application will establish a TCP/IP connection to the Network Server. See this doc for how to setup and operate the Network Server: http://db.apache.org/derby/docs/10.8/adminguide/
这篇关于无法在Eclipse中连接到Derby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!