无法启动Derby数据库

时间:2014-05-28 23:35:54

标签: java 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:无法启动数据库>' / Applications / blazeds / tomcat / webapps / testdrive / WEB-INF / database / game_db',有关详细信息,请参阅下一个>异常。 details = null rootCause = ASObject(23393258)>> {message = java.sql.SQLException:无法启动数据库>' / Applications / blazeds / tomcat / webapps / testdrive / WEB-INF / database / game_db& #39;,详见下一个>异常。,suppress = [],localizedMessage = java.sql.SQLException:无法>启动数据库' / Applications / blazeds / tomcat / webapps / testdrive / WEB- > INF / database / game_db',详见下一个异常。,cause = java.sql.SQLException}> body = null extendedData = null

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

之后我想德比可能会遇到并发连接问题。我看到嵌入式JDBC只能处理一个连接,所以我将驱动程序从Embedded更改为Client,这再次导致登录功能正常工作,另一个错误说连接中的url为null。有什么想法吗?提前感谢任何想法。

编辑:

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();
    }
}

}

1 个答案:

答案 0 :(得分:0)

嵌入模式下的多个连接没有问题。完全停止。

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