Created
May 31, 2012 22:08
-
-
Save rfelix/2846657 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
public class Connect | |
{ | |
public static void main (String[] args) | |
{ | |
Connection conn = null; | |
try | |
{ | |
String userName = "testuser"; | |
String password = "testpass"; | |
String url = "jdbc:mysql://localhost/test"; | |
Class.forName ("com.mysql.jdbc.Driver").newInstance (); | |
conn = DriverManager.getConnection (url, userName, password); | |
System.out.println ("Database connection established"); | |
} | |
catch (Exception e) | |
{ | |
System.err.println ("Cannot connect to database server"); | |
} | |
finally | |
{ | |
if (conn != null) | |
{ | |
try | |
{ | |
conn.close (); | |
System.out.println ("Database connection terminated"); | |
} | |
catch (Exception e) { /* ignore close errors */ } | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment