Guys i wanted to connect Java Application with MySql database.I have written a Java Programme.I have already created the database also.But it doesn't connect with the Database.This is my code.
import java.sql.*;
public class check
{
public static void main (String[] args)
{
Connection conn = null;
try
{
String userName = "root";
String password = "sss";
String url = "jdbc:mysql://localhost/musicdb";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
//Connection connection=getConnection();
Statement st=null;
ResultSet rs=null;
st=conn.createStatement();
rs=st.executeQuery("select count(*) from musicdata");
rs.next();
int musicCount=rs.getInt(1);
System.out.println("Music Count:"+musicCount);
}
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 */ }
}
}
}
}
Is this correct?