【发布时间】:2011-10-20 12:41:27
【问题描述】:
我正在尝试使用 eclipse 开发一个使用 derby 数据库并在 tomcat 上运行的 Web 应用程序。
我的问题是我无法使用 eclipse 启动 derby 服务器(它可以在 CMD 之外正常工作)并且我无法让我的 servlet 建立与数据库的连接,每次尝试时都会出现错误:
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 代码实现(这似乎更容易学习因为我还很新)。
我用来连接的代码如下。
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 中编写的东西来连接它。
【问题讨论】:
-
考虑到我的应用程序最终将被移动到我使用 eclipse 的服务器上,实际上只是为了简化以后打包的所有内容。
-
:/ 没有完成:P。我想知道是否有比 eclipse 更简单的方法来开发 java web 应用程序。