3

そのため、今週末にJDBCの学習を終了し、コードをmainメソッドからMVCアプリに正常に転送しました。このアプリケーションの目的は、プレーヤーの名簿を保持し、要求された場合にユーザーの資格情報を表示することです。プログラムはうまく機能し、私が次のようなURLを要求すると...

http://localhost:8084/gmustudent/players?id=1

そのプレーヤーの正しい出力が得られます!問題は、PlayersDAOクラス内でデータベース接続を実行していることです。これは、これを行うための「最良の」方法ではないと思います。だから私は2つの質問があります。

  1. web.xmlファイルまたはその他のファイル内でデータベース接続を実行して、サーバーが最初に起動されたときにデータベースへの接続をすぐに実行し、要求されたときにクエリを実行できるようにする方法はありますか?
  2. そして、これは実際にDAOで接続するよりも優れた代替手段ですか、それとも予期しないマイナスの欠点がありますか。AKAデータベースへの常時接続は、まさに私が望まないものでしょうか?

コメントやリンクをいただければ幸いです。そして、DAOクラス用に現在持っているコードを共有して、これまでのコードを確認できるようにします。皆さん、ありがとうございました!

package com.jdbc.test;

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;

public class PlayersDAO 
{
    public static Players viewPlayer(int id) throws SQLException
    {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        Players playerObject = null;

        try
        {
            String url = "jdbc:mysql://localhost:3306/gmustudent";
            String username = "root";
            String password = "root";

            try
            {
                Class.forName("com.mysql.jdbc.Driver");
            }
            catch(ClassNotFoundException error)
            {
                System.out.println("Error: " + error.getMessage());
            }

            connection = DriverManager.getConnection(url, username, password);
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT * FROM players WHERE id = " + id);

            if(resultSet.next())
                playerObject = new Players(resultSet.getLong("id"), resultSet.getString("name"), resultSet.getString("position"), resultSet.getString("height"), resultSet.getString("year"), resultSet.getString("hometown"), resultSet.getString("highschool"), resultSet.getString("headshot"));   
        }
        finally
        {
            if (connection != null) try{connection.close();} catch(SQLException ignore) {}
            if (statement != null) try{statement.close();} catch(SQLException ignore) {}
            if (resultSet != null) try{resultSet.close();} catch(SQLException ignore) {}
        }

        return playerObject;
    }
}
4

3 に答える 3

0

リソースを使用して、web.xmlファイルにデータベース接続を作成できます。このチュートリアルがお役に立てば幸いです。

http://viralpatel.net/blogs/database-connection-pooling-tomcat-eclipse-db/

于 2012-10-11T04:01:16.967 に答える
0

JDBC接続プーリングを使用できます。これにより、アプリケーションのパフォーマンス、同時実行性、およびスケーラビリティーの点で大きなメリットが得られます。

于 2012-10-11T04:33:00.127 に答える
0

IMHO, its always a good approach to hide the instantiation details of any resources. You can use factory method to do this. The advantage of this approach is that you can always change the way to manage the resources - e.g. You can have one instance of JDBCConnection per DAO or you can have connection pool. Any time you can provide your development specific db connection, test specific db connection or production db connection. All these details you can hide doing such approach.

于 2012-10-11T15:20:34.650 に答える