1 2 Previous Next 26 Replies Latest reply: Feb 22, 2007 9:01 AM by abillconsl RSS

    Can' t connect Java with MySQL

    807606
      My goal is to connect Java with MySQL. I found many solutions on Internet, but I always get the same mistake:
      SQLException: No suitable driver
      SQLState: 08001
      VendorError: 0
      MySQL works fine alone or with php.Only thing left me to think is that the installed versions are not compatible for this mysql-connector-java-5.0.4
      I don't believe that could be a reason.
      Installed versions are:
      Apache Tomcat 5.5.20 Server
      Apache HTTP Server 2.2.4
      PHP 5.2.0
      MySQL 5.2
      jre 1.5.0_11
      jdk1.5.0_11
      Apache Tomacat JK2 connector Version: 1.2.20 File Name: mod_jk-apache-2.2.3.so
      mysql-connector-java-5.0.4


      I also set connector in class path: C:\mysql-connector-java-5.0.4;C:\mysql-connector-java-5.0.4\mysql-connector-java-5.0.4-bin.jar;C:\mysql-connector-java-5.0.4\src\com\mysql\jdbc

      For installation I used manulas from:

      http://apacheguide.org/jsp.php
      http://doc.51windows.net/mysql/?url=/MySQL/ch23s03.html

      Here is also a test code in java:
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.SQLException;
      
      
         public class Connect
         {
             public static void main (String[] args)
             {
                 Connection conn = null;
      
                 try {
          conn = 
             DriverManager.getConnection("jdbc:mysql://localhost/first_test" + 
                                         "user=monty&password=greatsqldb");
      
          // Do something with the Connection
      
      
      } catch (SQLException ex) {
          // handle any errors
          System.out.println("SQLException: " + ex.getMessage());
          System.out.println("SQLState: " + ex.getSQLState());
          System.out.println("VendorError: " + ex.getErrorCode());
      }
             }
         }
      i'm desperate, please help or tell me someone who'll know the answer.
      Thank You in advance
        • 1. Re: Can' t connect Java with MySQL
          807606
          Your JDBC URL connection is bad.
          In particular, it is missing a "?" before the parameters user & password.
          • 2. Re: Can' t connect Java with MySQL
            807606
            Also you don't seem to be loading the Driver class (the thing with the Class.forName() you know..?)
            • 3. Re: Can' t connect Java with MySQL
              807606
              Sorry for my ignorance, but I really don't know how to load this class. Could you please tell more specific.
              And this:
              ...
               conn = 
                     DriverManager.getConnection("jdbc:mysql://localhost/first_test" +
                                                 "user=?root&password=?10000000");
              ...
              doesn't work
              I also tried
              conn = 
                     DriverManager.getConnection("jdbc:mysql://localhost/first_test" +
                                                 "?user=root&?password=10000000");
              and also doesn't work
              • 4. Re: Can' t connect Java with MySQL
                masijade
                Have you even tried to read the Connector/J documentation?

                There is a complete example there, and a single look at that example will solve your problem here.

                Do that, try again, then tell us if it worked.
                • 5. Re: Can' t connect Java with MySQL
                  807606
                  Sorry for my ignorance, but I really don't know how
                  to load this class. Could you please tell more
                  specific.
                  And this:
                  ...
                  conn = 
                  
                  riverManager.getConnection("jdbc:mysql://localhost/fir
                  st_test" +
                  
                  user=?root&password=?10000000");
                  ...
                  doesn't work
                  I also tried
                  conn = 
                  
                  riverManager.getConnection("jdbc:mysql://localhost/fir
                  st_test" +
                  
                  ?user=root&?password=10000000");
                  and also doesn't work
                  try this
                  conn = 
                         DriverManager.getConnection("jdbc:mysql://localhost/first_test?" +
                                                     "user=root&password=10000000");
                  • 6. Re: Can' t connect Java with MySQL
                    807606
                    OK, I'll do it again
                    • 7. Re: Can' t connect Java with MySQL
                      807606
                      for p_epi
                      I tried:
                      conn = 
                             DriverManager.getConnection("jdbc:mysql://localhost/first_test?" +
                                                         "user=root&password=10000000");
                      doesn't work!!
                      • 8. Re: Can' t connect Java with MySQL
                        jwenting
                        >
                        try this
                        conn = 
                        
                        riverManager.getConnection("jdbc:mysql://localhost/fir
                        st_test?" +
                        
                        user=root&password=10000000");
                        won't work either without first loading a driver...
                        • 9. Re: Can' t connect Java with MySQL
                          masijade
                          Like I said, did you read the documentation.

                          You are not loading the driver, so the driver manager cannot find the driver. That is the reason for the "no suitable driver" error. At least now, you finally have the url correct, so that solves a future problem, but the current problem is the lack of the "Class.forName" call, that has already been mentioned.

                          And, as I have said a few times already, a single look at the documentation would have pointed this out in 5 seconds. Now do that this time, and then try again.

                          Edit:

                          And I am just too slow (or just simply too long winded).
                          • 10. Re: Can' t connect Java with MySQL
                            807606
                            you should register the driver first
                            import java.sql.Connection;
                            import java.sql.DriverManager;
                            import java.sql.SQLException;
                             
                            public class Connect
                            {
                            public static void main (String[] args)
                            {
                            Connection conn = null;
                             
                             try {
                            //register the driver.....
                            
                            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
                            conn = 
                            DriverManager.getConnection(
                            "jdbc:mysql://localhost/first_test?" + 
                            "user=root&password=10000000");
                            // Do something with the Connection
                            } catch (SQLException ex) {
                            // handle any errors
                            System.out.println("SQLException: " + ex.getMessage());
                            System.out.println("SQLState: " + ex.getSQLState());
                            System.out.println("VendorError: " + ex.getErrorCode());
                            }
                            }
                            }
                            Message was edited by:
                            p_epi

                            Message was edited by:
                            p_epi
                            • 11. Re: Can' t connect Java with MySQL
                              807606
                              hey buddy .. it seems yr code is wrong .. in getconnection () method u should also specify the port ,which u r not doing ...

                              the default port for MySQL is 3306 ... see below i am giving you a sample code ... its working fine .. and dont forget to put the MySQL driver jar path in to classpath and also copy the jar into common/lib folder of your tomcat ....


                              import java.sql.Connection;
                              import java.sql.DriverManager;
                              import java.sql.ResultSet;
                              import java.sql.SQLException;
                              import java.sql.Statement;

                              public class MySQLConnectionTest {

                                   public static void main(String[] args) {
                              new MySQLConnectionTest().connTest();
                              }

                              public void connTest() {

                                        String tableName = "portfolio"; //change as per setting
                                        String hostName = "10.81.9.39"; // please change for the target database ip or hostname
                                        String dbPort = "3306"; //change if not using the default
                                        String dbName = "tradingsystem"; //change as per the given DB name
                                        String username = "root"; //change as per setting
                                        String password = "password"; //change as per setting
                                        System.out.println("before try");
                                        
                                        Double data=0.0;
                                   Double data1=0.0;
                                   

                                        try {
                              Class.forName("org.gjt.mm.mysql.Driver");

                                             System.out.println("before driver manager");
                              Connection conn = DriverManager.getConnection("jdbc:mysql://"+hostName+":"+dbPort+"/"+dbName, username, password);

                              String query1 = "select * from "+tableName+" where User_id='trader1' and Stock_Type='Equity'";
                              System.out.println("quesry1="+query1);

                              Statement stmt = conn.createStatement();

                              ResultSet rs1 = stmt.executeQuery(query1);

                              while(rs1.next())
                              {
                                   System.out.println("hiiiiii for rs1");
                                   System.out.println(rs1);
                                   Quantity=(Integer)rs1.getObject(5);
                                   MarketPrice=(Double) rs1.getObject(8);
                                   data=Quantity*MarketPrice;
                                   data1+=data;

                              }     
                                   System.out.println("data1="+data1);
                                   i=0;
                                   
                              rs1.close();
                              stmt.close();
                              conn.close();
                              } catch (ClassNotFoundException e) {
                              e.printStackTrace(System.err);
                              } catch (SQLException e) {
                              e.printStackTrace(System.err);
                              }
                                   }
                              }



                              i hope it will work for u...
                              cheers,
                              • 12. Re: Can' t connect Java with MySQL
                                807606
                                OK, I've read the instructions (23.4.5.1.1. Connecting to MySQL Using the DriverManager Interface). Maybe I haven't understood it correctly again because it doesn't work. I made corrections so my code look like this!
                                import java.sql.Connection;
                                import java.sql.DriverManager;
                                import java.sql.SQLException;
                                
                                
                                   public class Connect
                                   {
                                       public static void main (String[] args)
                                       {
                                            try {
                                            // The newInstance() call is a work around for some
                                            // broken Java implementations
                                
                                            Class.forName("com.mysql.jdbc.Driver").newInstance();
                                        } catch (Exception ex) {
                                            // handle the error
                                        }
                                        
                                           Connection conn = null;
                                
                                           try {
                                                
                                                    conn = 
                                       DriverManager.getConnection("jdbc:mysql://localhost/first_test?" +
                                                                   "user=root&password=10000000");
                                
                                    // Do something with the Connection
                                
                                
                                } catch (SQLException ex) {
                                    // handle any errors
                                    System.out.println("SQLException: " + ex.getMessage());
                                    System.out.println("SQLState: " + ex.getSQLState());
                                    System.out.println("VendorError: " + ex.getErrorCode());
                                }
                                       }
                                   }
                                I get the same result: No suitable driver.... If I use the code that p_epi suggested I get the error after compiling:
                                Connect.java:14: package com.mysql.jdbc does not exist
                                                      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
                                1 error
                                What to do NOW ?
                                • 13. Re: Can' t connect Java with MySQL
                                  807606
                                  Have you downloaded the mysql jdbc drivers and are they in the classpath?
                                  • 14. Re: Can' t connect Java with MySQL
                                    807606
                                    I also set connector in class path: C:\mysql-connector-java-5.0.4;C:\mysql-connector-java-5.0.4\mysql-connector-java-5.0.4-bin.jar;C:\mysql-connector-java-5.0.4\src\com\mysql\jdbc
                                    1 2 Previous Next