java - 启动 Derby 数据库失败

标签 java apache-flex tomcat jdbc derby

所以我对 Java 和 Derby 还很陌生。我在 Tomcat 7 上将两者与我的 Flex 应用程序一起使用。

当我从 Flex 调用 Java 时,登录功能正常工作,但我的 getUserByUsername 功能却没有。

public Boolean loginUser(String username, String password) throws Exception
{
    Connection c = null;
    String hashedPassword = new String();

    try
    {
        c = ConnectionHelper.getConnection();

        PreparedStatement ps = c.prepareStatement("SELECT password FROM users WHERE username=?");
        ps.setString(1, username);

        ResultSet rs = ps.executeQuery();

        if(rs.next())
        {
            hashedPassword = rs.getString("password");
        }
        else
        {
            return false;
        }

        if(Password.check(password, hashedPassword))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    catch (SQLException e)
    {
        e.printStackTrace();throw new DAOException(e);
    }
    finally
    {
        ConnectionHelper.closeConnection(c);
    }
}

public User getUserByUsername(String username) throws DAOException
{
    //System.out.println("Executing DAO.getUserByName:" + username);

    User user = new User();

    Connection c = null;
    try
    {

        c = ConnectionHelper.getConnection();

        PreparedStatement ps = c.prepareStatement("SELECT * FROM users WHERE username = ?");
        ps.setString(1, username);

        ResultSet rs = ps.executeQuery();

        while(rs.next())
        {
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setTeam(rs.getString("team"));
            user.setScore(rs.getInt("score"));
        }
    }
    catch (SQLException e)
    {
        e.printStackTrace();
        throw new DAOException(e);
    }
    finally
    {
        ConnectionHelper.closeConnection(c);
    }
    return user;
}

据我所知,我在 Flex 中获得的堆栈毫无用处:

Flex Message (flex.messaging.messages.ErrorMessage) clientId = 8EB6D37B-7E0B-01B0->AA55-457722B9036C correlationId = A39E574F-CFC6-51FE-6CBE-451AF329E2F8 destination >= service messageId = 8EB6DF4C-650B-BDD7-7802-B813A61C8DC8 timestamp = >1401318734645 timeToLive = 0 body = null code = Server.Processing message = >services.DAOException : java.sql.SQLException: Failed to start database >'/Applications/blazeds/tomcat/webapps/testdrive/WEB-INF/database/game_db', see the next >exception for details. details = null rootCause = ASObject(23393258)>>{message=java.sql.SQLException: Failed to start database >'/Applications/blazeds/tomcat/webapps/testdrive/WEB-INF/database/game_db', see the next >exception for details., suppressed=[], localizedMessage=java.sql.SQLException: Failed to >start database '/Applications/blazeds/tomcat/webapps/testdrive/WEB->INF/database/game_db', see the next exception for details., cause=java.sql.SQLException} >body = null extendedData = null

我的第一个想法是,这只是我的函数中的一个错误(也许其他人会注意到它),但我已经查看了几个小时,但我什么也看不到。

在那之后,我认为 Derby 可能在并发连接方面存在问题。我在某个地方看到嵌入式 JDBC 只能处理一个连接,所以我将驱动程序从嵌入式更改为客户端,这再次导致登录功能正常工作,另一个错误表明连接中的 url 为空。有什么想法吗?提前感谢任何想法。

编辑:

package services;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.net.URLDecoder;

public class ConnectionHelper
{

private String url;

private static ConnectionHelper instance;

public String getUrl()
{
    return url;
}

private ConnectionHelper()
{
    try
    {
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
        String str = URLDecoder.decode(getClass().getClassLoader().getResource("services").toString(),"UTF-8");
        str= str.substring(0, str.indexOf("classes/services")); 
        if ( str.startsWith("file:/C:",0)){
            str=str.substring(6);
        }
        else{
            str=str.substring(5);
        }
        url = "jdbc:derby:" + str + "database/game_db";
        System.out.println("Database url "+url);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

public static ConnectionHelper getInstance()
{
    if (instance == null)
        instance = new ConnectionHelper();
    return instance;
}

public static Connection getConnection() throws java.sql.SQLException
{
    return DriverManager.getConnection(getInstance().getUrl());
}

public static void closeConnection(Connection c)
{
    try
    {
        if (c != null)
        {
            c.close();
        }
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }
}

}

最佳答案

嵌入模式下多连接没有问题。句号。

也就是说,您可能遇到过,一次只有一个 jvm 进程 可以访问 Derby 数据库文件。但是那个 jvm 可能有 1000 个线程,每个线程都有自己与 Derby 的连接(当然,在资源允许的情况下)。

关于java - 启动 Derby 数据库失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23923534/

相关文章:

apache-flex - 在ActionScript和Flex中计算文本宽度

android - Android 上的 Flex AIR 应用程序能否在启动时作为服务启动?

java - 显示两个 Java 类

java - Java 中的 Websocket 客户端 - NoClassDefFoundError

ios - 在iOS上使用ActionScript3播放声音会导致不良效果

java - 如何使用 Eclipse Indigo 在 Tomcat7 中部署 Web 应用程序?

java - 如何控制第 3 方库中的日志记录

java - 部署tomcat 7时出现PermGen Space错误?

java - 优化程序以查找数组中的第 k 个最小元素(Java,预期时间复杂度 O(n))

java - 当我尝试粘贴某些内容时,_pasted_code_ 是什么