「Java入門」JDBCドライバでMySQLへ接続するサンプル

Javaコード:
import java.sql.*;
public class DBConnectDemo {
public static void main(String[] args) {
Connection con = null;
try {
Class.forName(“com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/demodb", “user001", “123456");
System.out.println(“DBに接続成功しました。");
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
System.out.println(“JDBC Driverのロードに失敗しました。");
} catch (SQLException e) {
System.out.println(“DBに接続失敗しました。");
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
System.out.println(“DBがクローズしました。");
}
}
}
}
}

Java

Posted by arkgame