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?

That is because you are pretty much ignoring the exception it throws (line 28 and line 39) which is a bad thing. Never catch exceptions only to replace them with "print" statements or worst, with an empty catch block. Till you incorporate a logger in your application (which IMO every non-trivial application should have), your best bet would be to dump the entire stack trace at the time the exception was thrown.

Do a 'e.printStackTrace()' of the exception and post the entire exception text here.

Edited 5 Years Ago by ~s.o.s~: n/a

Comments
Who need to listen for exceptions, their are pointless ;)

@pro learner. As SOS suggested you need to put e.printStackTrace() in your code and its good to make it as habit whenever you write the code.

And i think you should specify the port number of your mysql in line 13.

The default port number of mysql is 3306.So assuming that you dint change the post number manually , it should be as follows

String url = "jdbc:mysql://localhost:3306/musicdb";

Edited 5 Years Ago by harinath_2007: n/a

Declare database too:

String db = "yourdatabase";

and add database like seen here:

conn = DriverManager.getConnection (url + db, userName, password);

Also, add port number and

e.printStackTrace()

as mentioned above.

Declare database too:

String db = "yourdatabase";

and add database like seen here:

conn = DriverManager.getConnection (url + db, userName, password);

Also, add port number and

e.printStackTrace()

as mentioned above.

I guess he already added the database name.

String url = "jdbc:mysql://localhost/musicdb";

musicdb is database name.

Yes,i have already declared the DB.And before this code,i tried e.printStackTrace() one.But it also didn't worked.That was why i moved to this way.

How did printStackTrace "not work". You need to explain that one more. In any case, add a printStackTrace anyway and post the results of that.

Edited 5 Years Ago by masijade: n/a

Anyway finally it solved by my own....:):)

Edited 5 Years Ago by pro_learner: n/a

Anyway finally it solved by my own

Oh. that's great..!:) If possible , post what went wrong and what did you do to solve so that others who encounter the same problem will be benifited.

This question has already been answered. Start a new discussion instead.