// ドライバのドキュメントを参照 String dbDriver = "com.mysql.jdbc.Driver"; // 接続文字列 String url= "jdbc:mysql://dbhost/xxxx"; String dbuser = "ssuser"; String dbpass = "ssuser"; Connection conn = null; Statement stmt = null; ResultSet rs = null; String row = null; try { // ドライバをロードする // エラーになるときは、ドライバへのクラスパスを確認のこと Class.forName( dbDriver ).newInstance(); // 接続 conn = DriverManager.getConnection(url, dbuser, dbpass); stmt = conn.createStatement(); // 問い合わせ rs = stmt.executeQuery("SELECT VERSION();"); rs.next(); row = rs.getString(1); } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null){ rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } }catch(Exception e) { } } |