Advertisement
droidus

Untitled

Nov 18th, 2011
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1.    import java.sql.*;
  2.  
  3.    public class sql
  4.    {
  5.        public static void main (String[] args)
  6.        {
  7.            Connection conn = null;
  8.  
  9.            try
  10.            {
  11.                String userName = "uname";
  12.                String password = "pword";
  13.                // String url = "jdbc:mysql://localhost/test";
  14.                String url = "jdbc:mysql://localhost/test";
  15.                Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  16.                conn = DriverManager.getConnection (url, userName, password);
  17.                System.out.println ("Database connection established");
  18.            }
  19.            catch (Exception e)
  20.            {
  21.                System.err.println ("Cannot connect to database server");
  22.            }
  23.            finally
  24.            {
  25.                if (conn != null)
  26.                {
  27.                    try
  28.                    {
  29.                        conn.close ();
  30.                        System.out.println ("Database connection terminated");
  31.                    }
  32.                    catch (Exception e) { /* ignore close errors */ }
  33.                }
  34.            }
  35.        }
  36.    }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement