My-Sql database connection



 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
import java.sql.*;

public class Mysql
{
    public static void main(String[] rk)
    {
        Connection  con=null;       
        Statement stmt=null;
        ResultSet rs=null;

        try
        {

             Class.forName("com.mysql.jdbc.Driver");
  con=DriverManager.getConnection("jdbc:mysql://localhost/mytables","root","magnet");
    
             stmt=con.createStatement();
    
             rs=stmt.executeQuery("select * from contacts");
   
 
  while(rs.next())
                   {
                     System.out.println(rs.getString(1));
                   }
        }catch(Exception e)
        {
                System.out.println("Error : "+e);  
        }
    }

}

/* Copy your MySql Connector .jar file into "C:\Program Files\Java\jdk1.6.0_06\jre\lib\ext" location or set the classpath for the same  .jar file*/
/* Create one user into your MySql Server or use the default user name and password (username="root password="root") */