import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Arrays; public class MySqlConnectTest { public static void main(String[] args) { String[][] dbInfoArrays = new String[][]{ {"jdbc:mysql://localhost:3306/root_db", "root", "*********"}, {"jdbc:mysql://localhost:3306/test_db", "tester", "******"}, {"jdbc:mysql://**.***.*.**:3306/root_db", "root", "*********"}, {"jdbc:mysql://**.***.*.**:3306/test_db", "tester", "******"}}; for (String[] dbInfoArray : dbInfoArrays) { Connection conn = null; Statement st = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(dbInfoArray[0], dbInfoArray[1], dbInfoArray[2]); st = conn.createStatement(); rs = st.executeQuery("select * from user_info"); while (rs.next()) { System.out.println(rs.getString(1) + "\t" + rs.getInt(2) + "\t" + "接続情報 = " + Arrays.deepToString(dbInfoArray)); } } catch (ClassNotFoundException clsEx) { clsEx.printStackTrace(); } catch (SQLException sqlEx) { System.out.println("接続失敗/接続情報 = " + Arrays.deepToString(dbInfoArray)); sqlEx.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } try { if (st != null) { st.close(); } } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } try { if (conn != null) { conn.close(); } } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } } } } }