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