jdbc/mysql connection problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bohiyaanam
    New Member
    • Jul 2007
    • 27

    jdbc/mysql connection problem

    hi!
    I am from india .I am new to Java.Presentlly i am working on project in which i need to use mysql.
    I had set up an development env.for the same .I had installed mmsql jdbc driver also.
    i am using apache-tomcat-jsp-mysql.
    but when i ran an servlet which includes mysql db it gives error like
    "Access denied for user 'root'@'localho st' (using password: YES)"
    i had tried so many way to resolve this but not done.
    Will u help me 2 come out of this problem.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by bohiyaanam
    hi!
    I am from india .I am new to Java.Presentlly i am working on project in which i need to use mysql.
    I had set up an development env.for the same .I had installed mmsql jdbc driver also.
    i am using apache-tomcat-jsp-mysql.
    but when i ran an servlet which includes mysql db it gives error like
    "Access denied for user 'root'@'localho st' (using password: YES)"
    i had tried so many way to resolve this but not done.
    Will u help me 2 come out of this problem.
    Most probably your password for root is wrong.
    Are you able to connect from the console using
    mysql -uroot -p ?

    Comment

    • bohiyaanam
      New Member
      • Jul 2007
      • 27

      #3
      yes,i am using that way only.
      But when i ran servlet from browser i get that error in my logs/catalina.out .

      Comment

      • bohiyaanam
        New Member
        • Jul 2007
        • 27

        #4
        when i go through consol i could use database.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by bohiyaanam
          yes,i am using that way only.
          But when i ran servlet from browser i get that error in my logs/catalina.out .
          Then there's something wrong with your connection string. Post the Connection string you're using.
          P.S You can mask the password with **

          Comment

          • bohiyaanam
            New Member
            • Jul 2007
            • 27

            #6
            connection string is:->http://localhost/examples/servlets/servlet/ShowBedrock

            ShowBedrock is my java file in which i accessing mysql DB.

            Comment

            • bohiyaanam
              New Member
              • Jul 2007
              • 27

              #7
              & in my servlet i am defining connection dbcon as ->
              "jdbc:mysql ://localhost:3306/bedrock","usern ame","supersecr et"**

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by bohiyaanam
                & in my servlet i am defining connection dbcon as ->
                "jdbc:mysql ://localhost:3306/bedrock","usern ame","supersecr et"**
                It must be the password. Tripple check the username and password again in the servlet.

                Comment

                • bohiyaanam
                  New Member
                  • Jul 2007
                  • 27

                  #9
                  yes
                  username is login username & supersecret is login password

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by bohiyaanam
                    yes
                    username is login username & supersecret is login password
                    The stacktrace said you're using "root" for username.

                    Comment

                    • bohiyaanam
                      New Member
                      • Jul 2007
                      • 27

                      #11
                      no username is Dude1
                      this to access database

                      Comment

                      • bohiyaanam
                        New Member
                        • Jul 2007
                        • 27

                        #12
                        & given all privilages to Dude1

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by bohiyaanam
                          no username is Dude1
                          this to access database
                          Put Dude1 for the username then? Your stacktrace is not matching what you're describing.

                          Comment

                          • bohiyaanam
                            New Member
                            • Jul 2007
                            • 27

                            #14
                            see i am posting u the complete code.Note that my Mysql login name =Dude1 & login password = "supersecre t"
                            ############### ############### ###############
                            [CODE=java] // File: ShowBedrock.jav a

                            /* A servlet to display the contents of the MySQL Bedrock database */

                            import java.io.*;
                            import java.net.*;
                            import java.sql.*;
                            import java.text.*;
                            import java.util.*;
                            import javax.servlet.* ;
                            import javax.servlet.h ttp.*;

                            public class ShowBedrock extends HttpServlet
                            {
                            public String getServletInfo( )
                            {
                            return "Servlet connects to MySQL database and displays result of a SELECT";
                            }

                            // Use http GET

                            public void doGet(HttpServl etRequest request, HttpServletResp onse response)
                            throws IOException, ServletExceptio n
                            {
                            String loginUser ="Dude1";
                            String loginPasswd = "supersecre t";
                            String loginUrl = "jdbc:mysql ://192.168.150.122 :3306/bedrock";

                            response.setCon tentType("text/html"); // Response mime type

                            // Output stream to STDOUT
                            PrintWriter out = response.getWri ter();

                            Bedrock out.println("<H TML>");
                            out.println("<B ODY>Bedrock

                            ");
                            out.println("<B ODY>Bedrock_b4_try

                            ");
                            // Load the JDBC/MySQL driver
                            try
                            {
                            out.println("<B ODY>Bedrock_inside_ try

                            ");

                            out.println( "<tr> => loading driver: " );
                            Class.forName(" com.mysql.jdbc. Driver").newIns tance();
                            out.println( "<tr> ok " );

                            out.println("<t r>" +
                            "<td>" + loginUrl + "" +
                            "<td>" + loginUser + "" +
                            "<td>" + loginPasswd + "" +
                            "</tr>" );

                            out.println("<t r> => connecting : ");
                            Connection dbcon = DriverManager.g etConnection(lo ginUrl, loginUser, loginPasswd);
                            // Connection dbcon = DriverManager.g etConnection("j dbc:mysql://192.168.150.122 :3306/bedrock","Dude1 ","supersecret" );
                            out.println("<t r> ok " );
                            out.println("<t r>" +
                            "<td>" + loginUrl + "" +
                            "<td>" + loginUser + "" +
                            "<td>" + loginPasswd + "" +
                            "</tr>" );
                            // Declare our statement
                            Statement statement = dbcon.createSta tement();

                            String query = "SELECT name, dept, ";
                            query += " jobtitle ";
                            query += "FROM employee ";

                            // Perform the query
                            ResultSet rs = statement.execu teQuery(query);

                            out.println("<T ABLE border>");

                            // Iterate through each row of rs
                            while (rs.next())
                            {
                            String m_name = rs.getString("n ame");
                            String m_dept = rs.getString("d ept");
                            String m_jobtitle = rs.getString("j obtitle");
                            out.println("<t r>" +
                            "<td>" + m_name + "" +
                            "<td>" + m_dept + "" +
                            "<td>" + m_jobtitle + "" +
                            "</tr>");
                            }

                            out.println("</TABLE>");

                            rs.close();
                            statement.close ();
                            dbcon.close();
                            }
                            catch (SQLException ex) {
                            while (ex != null) {
                            System.out.prin tln ("SQL Exception: " + ex.getMessage ());
                            ex = ex.getNextExcep tion ();
                            } // end while
                            } // end catch SQLException

                            catch(java.lang .Exception ex)
                            {
                            out.println("<H TML>" +
                            " + "<HEAD>
                            "Bedrock: Error" +
                            "</TITLE>\n" +
                            "<P>SQL error in doGet: " +
                            ex.getMessage() + "</P>");
                            return;
                            }
                            out.close();
                            }
                            }[/CODE]
                            Last edited by r035198x; Jul 11 '07, 11:37 AM. Reason: added code tags

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by bohiyaanam
                              see i am posting u the complete code.Note that my Mysql login name =Dude1 & login password = "supersecre t"
                              ############### ############### ###############
                              // File: ShowBedrock.jav a

                              /* A servlet to display the contents of the MySQL Bedrock database */

                              import java.io.*;
                              import java.net.*;
                              import java.sql.*;
                              import java.text.*;
                              import java.util.*;
                              import javax.servlet.* ;
                              import javax.servlet.h ttp.*;

                              public class ShowBedrock extends HttpServlet
                              {
                              public String getServletInfo( )
                              {
                              return "Servlet connects to MySQL database and displays result of a SELECT";
                              }

                              // Use http GET

                              public void doGet(HttpServl etRequest request, HttpServletResp onse response)
                              throws IOException, ServletExceptio n
                              {
                              String loginUser ="Dude1";
                              String loginPasswd = "supersecre t";
                              String loginUrl = "jdbc:mysql ://192.168.150.122 :3306/bedrock";

                              response.setCon tentType("text/html"); // Response mime type

                              // Output stream to STDOUT
                              PrintWriter out = response.getWri ter();

                              out.println("<H TML><HEAD><TITL E>Bedrock</TITLE></HEAD>");
                              out.println("<B ODY><H1>Bedrock </H1>");
                              out.println("<B ODY><H1>Bedrock _b4_try</H1>");
                              // Load the JDBC/MySQL driver
                              try
                              {
                              out.println("<B ODY><H1>Bedrock _inside_try</H1>");

                              out.println( "<tr><td> => loading driver: </td></tr>" );
                              Class.forName(" com.mysql.jdbc. Driver").newIns tance();
                              out.println( "<tr><td> ok </td></tr>" );

                              out.println("<t r>" +
                              "<td>" + loginUrl + "</td>" +
                              "<td>" + loginUser + "</td>" +
                              "<td>" + loginPasswd + "</td>" +
                              "</tr>" );

                              out.println("<t r><td> => connecting : </td></tr>");
                              Connection dbcon = DriverManager.g etConnection(lo ginUrl, loginUser, loginPasswd);
                              // Connection dbcon = DriverManager.g etConnection("j dbc:mysql://192.168.150.122 :3306/bedrock","Dude1 ","supersecret" );
                              out.println("<t r><td> ok </td></tr>" );
                              out.println("<t r>" +
                              "<td>" + loginUrl + "</td>" +
                              "<td>" + loginUser + "</td>" +
                              "<td>" + loginPasswd + "</td>" +
                              "</tr>" );
                              // Declare our statement
                              Statement statement = dbcon.createSta tement();

                              String query = "SELECT name, dept, ";
                              query += " jobtitle ";
                              query += "FROM employee ";

                              // Perform the query
                              ResultSet rs = statement.execu teQuery(query);

                              out.println("<T ABLE border>");

                              // Iterate through each row of rs
                              while (rs.next())
                              {
                              String m_name = rs.getString("n ame");
                              String m_dept = rs.getString("d ept");
                              String m_jobtitle = rs.getString("j obtitle");
                              out.println("<t r>" +
                              "<td>" + m_name + "</td>" +
                              "<td>" + m_dept + "</td>" +
                              "<td>" + m_jobtitle + "</td>" +
                              "</tr>");
                              }

                              out.println("</TABLE>");

                              rs.close();
                              statement.close ();
                              dbcon.close();
                              }
                              catch (SQLException ex) {
                              while (ex != null) {
                              System.out.prin tln ("SQL Exception: " + ex.getMessage ());
                              ex = ex.getNextExcep tion ();
                              } // end while
                              } // end catch SQLException

                              catch(java.lang .Exception ex)
                              {
                              out.println("<H TML>" +
                              "<HEAD><TIT LE>" +
                              "Bedrock: Error" +
                              "</TITLE></HEAD>\n<BODY>" +
                              "<P>SQL error in doGet: " +
                              ex.getMessage() + "</P></BODY></HTML>");
                              return;
                              }
                              out.close();
                              }
                              }
                              And what's the exception when you run it like that?

                              Comment

                              Working...