<%@ page contentType="text/html; charset=Shift_JIS" %> <%@ page import="java.sql.*" %> <% Connection con = null; Statement stmt = null; try { // ドライバクラスをロード Class.forName("org.gjt.mm.mysql.Driver"); // データベースへ接続 String url = "jdbc:mysql:///hellodb?useUnicode=true&characterEncoding=SJIS"; con = DriverManager.getConnection(url); // ステートメントオブジェクトを生成 stmt = con.createStatement(); // 全ての行を検索するSQL文を作成 String sql = "SELECT * FROM HELLO_WORLD_TABLE"; // クエリーを実行して結果セットを取得 ResultSet rs = stmt.executeQuery(sql); %> <html> <head> <title>JSPでDB接続</title> </head> <body> <table border="1"> <tr> <th>NO</th> <th>言語</th> <th>メッセージ</th> </tr> <% // 検索された行数分ループ while(rs.next()){ %> <tr> <td><%=rs.getInt("NO") %></td> <td><%=rs.getString("LANGUAGE") %></td> <td><%=rs.getString("MESSAGE") %></td> </tr> <% } // end while %> </table> </body> </html> <% } catch (Exception e) { out.println("<font color=red><h3>エラー!</h3></font>" + e); e.printStackTrace(); } finally { // データベースへの接続をクローズします try { if (stmt!=null) { stmt.close(); } if (con!=null) { con.close(); } } catch (Exception e) { e.printStackTrace(); } } %>
package javahello; import java.sql.*; public class DatabaseAccess { private String driver; private String url; private String user; private String passwd; private Connection con; private Statement stmt; private ResultSet rs; /** * コンストラクタ */ public DatabaseAccess() { driver = "org.gjt.mm.mysql.Driver"; url = "jdbc:mysql:///hellodb?useUnicode=true&characterEncoding=SJIS"; user = ""; passwd = ""; } /** * データベースに接続 */ public synchronized void open() throws Exception { // ドライバクラスをロード Class.forName(driver); // データベースへ接続 con = DriverManager.getConnection(url,user,passwd); // ステートメントオブジェクトを生成 stmt = con.createStatement(); } /** * データベースから切断 */ public synchronized void close() throws Exception { if (stmt!=null) { stmt.close(); } if (con!=null) { con.close(); } } /** * クエリーを実行(検索) */ public ResultSet executeQuery(String sql) throws Exception { return stmt.executeQuery(sql); } /** * クエリーを実行(新規、更新、削除) */ public int executeUpdate(String sql) throws Exception { return stmt.executeUpdate(sql); } }
C:\JavaHello\JSPDB>
<%@ page contentType="text/html; charset=Shift_JIS" %> <%@ page import="java.sql.*" %> <jsp:useBean id="db" class="javahello.DatabaseAccess" /> <% try { // データベースに接続 db.open(); // 全ての行を検索するSQL文を作成 String sql = "SELECT * FROM HELLO_WORLD_TABLE"; // クエリーを実行して結果セットを取得 ResultSet rs = db.executeQuery(sql); %> <html> <head> <title>JSPでDB接続</title> </head> <body> <table border="1"> <tr> <th>NO</th> <th>言語</th> <th>メッセージ</th> </tr> <% // 検索された行数分ループ while(rs.next()){ %> <tr> <td><%=rs.getInt("NO") %></td> <td><%=rs.getString("LANGUAGE") %></td> <td><%=rs.getString("MESSAGE") %></td> </tr> <% } // end while %> </table> </body> </html> <% } catch (Exception e) { out.println("<font color=red><h3>エラー!</h3></font>" + e); e.printStackTrace(); } finally { // データベースへの接続をクローズします try { db.close(); } catch (Exception e) { e.printStackTrace(); } } %>
package javahello; import java.sql.*; import java.util.*; public class DBConnectionPool { private String driver; // ドライバクラス private String url; // URL private String user; // ユーザー名 private String password; // パスワード private int maxConnection; // 最大接続数 private int checkedOut; // 貸し出している接続数 private Vector connectionPool = new Vector(); private static DBConnectionPool instance; /** * インスタンスを取得 */ public static synchronized DBConnectionPool getInstance() { if (instance == null) { instance = new DBConnectionPool(); } return instance; } /** * コンストラクタ */ private DBConnectionPool() { this.driver = "org.gjt.mm.mysql.Driver"; this.url = "jdbc:mysql:///hellodb?useUnicode=true&characterEncoding=SJIS"; this.user = ""; this.password = ""; this.maxConnection = 10; } /** * コネクションを取得 */ public synchronized Connection getConnection() throws Exception { Connection con = null; if (connectionPool.size() > 0) { con = (Connection) connectionPool.firstElement(); connectionPool.removeElementAt(0); try { if (con.isClosed()) { con = getConnection(); } } catch (SQLException e) { con = getConnection(); } } else if (maxConnection == 0 || checkedOut < maxConnection) { con = newConnection(); } if (con != null) { checkedOut++; } return con; } /** * 新規にコネクションを作成 */ private Connection newConnection() throws Exception { Class.forName(driver); return DriverManager.getConnection(url, user, password); } /** * コネクションを返却 */ public synchronized void freeConnection(Connection con) { connectionPool.addElement(con); checkedOut--; notifyAll(); } /** * すべてのコネクションを開放 */ public synchronized void release() { Enumeration enumConnections = connectionPool.elements(); while (enumConnections.hasMoreElements()) { Connection con = (Connection)enumConnections.nextElement(); try { con.close(); } catch (SQLException e) { } } connectionPool.removeAllElements(); } }
<%@ page contentType="text/html; charset=Shift_JIS" %> <%@ page import="java.sql.*,javahello.DBConnectionPool" %> <%! DBConnectionPool pool; %> <% Connection con = null; Statement stmt = null; try { // コネクションプールクラスのインスタンスを取得 pool = DBConnectionPool.getInstance(); // データベースへの接続を取得 con = pool.getConnection(); // ステートメントオブジェクトを生成 stmt = con.createStatement(); // 全ての行を検索するSQL文を作成 String sql = "SELECT * FROM HELLO_WORLD_TABLE"; // クエリーを実行して結果セットを取得 ResultSet rs = stmt.executeQuery(sql); %> <html> <head> <title>JSPでDB接続</title> </head> <body> <table border="1"> <tr> <th>NO</th> <th>言語</th> <th>メッセージ</th> </tr> <% // 検索された行数分ループ while(rs.next()){ %> <tr> <td><%=rs.getInt("NO") %></td> <td><%=rs.getString("LANGUAGE") %></td> <td><%=rs.getString("MESSAGE") %></td> </tr> <% } // end while %> </table> </body> </html> <% } catch (Exception e) { out.println("<font color=red><h3>エラー!</h3></font>" + e); e.printStackTrace(); } finally { // データベースへの接続をクローズします try { if (stmt!=null) { stmt.close(); } } catch (Exception e) { e.printStackTrace(); } if (con!=null) { // データベースへの接続を返却します pool.freeConnection(con); } } %>