1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package test;
import java.sql.*;
public class Connect {
/**
*
*/
public static void main (String[] args) {
Connection conn = null ;
try {
String userName = "user" ;
String password = "pwd" ;
String url="jdbc:mysql://localhost:3306/mabase" ;
Class.forName("com.mysql.jdbc.Driver").newInstance() ;
conn = DriverManager.getConnection(url, userName, password) ;
System.out.println("DataBase Connection established") ;
}
catch(Exception e) {
System.out.println("Connection failed !"+e.toString()) ;
}
finally {
if (conn != null) {
try {
conn.close() ;
System.out.println("Database connexion terminated.") ;
}
catch (Exception e){
}
}
}
}
} |