Example of MySQL connection in Java

#
#   mysql-connector-java-3.0.11-stable-bin.jar or later must be downloaded
#   from:http://dev.mysql.com/downloads/connector/j/3.0.html
#
#   Compile:
#     javac Connect.java
#
#   Run:
#     java Connect
 
import java.sql.*;
 
public class Connect
{
	public static void main (String[ ] args)
	{
	Connection conn = null;
	String url = "jdbc:mysql://localhost/";
	String userName = "root";
	String password = "";
 
	try
	{
		Class.forName ("com.mysql.jdbc.Driver").newInstance ( );
		conn = DriverManager.getConnection (url, userName, password);
		System.out.println ("Connected");
	}
	catch (Exception e)
	{
		System.err.println ("Cannot connect to server");
	}
	finally
	{
		if (conn != null)
		{
			try
			{
				conn.close ( );
				System.out.println ("Disconnected");
			}
			catch (Exception e) { /* ignore close errors */ }
		}
	}
}

Download this snippet

Shows how to connecto to a MySQL database with Java.

Twitter Twitter

Tags

Java MySQL

5 Comments to “ Example of MySQL connection in Java”

  1. Anonymous  on Sep 25, 2009

    j5srLN <a href="http://vpsjzdebgcqi.com/">vpsjzdebgcqi</a>, [url=http://jtifmmcofskt.com/]jtifmmcofskt[/url], [link=http://csyexagtewgj.com/]csyexagtewgj[/link], http://ovcukchzqfvt.com/

  2. Anonymous  on Feb 21, 2010

    pprdjC <a href="http://zvcpnnxkahrv.com/">zvcpnnxkahrv</a>, [url=http://azzvhvkhokke.com/]azzvhvkhokke[/url], [link=http://oqyhrnbfqkpw.com/]oqyhrnbfqkpw[/link], http://pfmmlnblhlgk.com/

  3. Anonymous  on Feb 21, 2010

    pprdjC <a href="http://zvcpnnxkahrv.com/">zvcpnnxkahrv</a>, [url=http://azzvhvkhokke.com/]azzvhvkhokke[/url], [link=http://oqyhrnbfqkpw.com/]oqyhrnbfqkpw[/link], http://pfmmlnblhlgk.com/

  4. Anonymous  on Feb 21, 2010

    pprdjC <a href="http://zvcpnnxkahrv.com/">zvcpnnxkahrv</a>, [url=http://azzvhvkhokke.com/]azzvhvkhokke[/url], [link=http://oqyhrnbfqkpw.com/]oqyhrnbfqkpw[/link], http://pfmmlnblhlgk.com/

  5. Anonymous  on Mar 14, 2010

    hvaBrL <a href="http://spvsitqzdlmh.com/">spvsitqzdlmh</a>, [url=http://gsoewojgkmku.com/]gsoewojgkmku[/url], [link=http://whjgmtmfeqay.com/]whjgmtmfeqay[/link], http://hmwntaehjmuy.com/

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>