Results 1 to 2 of 2
  1. #1
    Rafiqul is offline Member
    Join Date
    Mar 2013
    Location
    Bangladesh
    Posts
    12
    Rep Power
    0

    Question MySQL (WampServer) connectivity with Java program using JDBC

    I am trying to run the following Java program for displaying contents of a table from a MySQL database:
    Java Code:
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    
    public class DisplayAuthors 
    {                           
       static final String DATABASE_URL = "jdbc:mysql://localhost/books";
       public static void main( String args[] )
       {
          Connection connection = null;
          Statement statement = null;
          ResultSet resultSet = null;
    
          try 
          {                         
             connection = DriverManager.getConnection( DATABASE_URL, "root", "" );
             statement = connection.createStatement();                                  
             resultSet = statement.executeQuery("SELECT authorID, firstName, lastName FROM authors" );
             ResultSetMetaData metaData = resultSet.getMetaData();
             int numberOfColumns = metaData.getColumnCount();     
             System.out.println( "Authors Table of Books Database:\n" );
             
             for ( int i = 1; i <= numberOfColumns; i++ )
                System.out.printf( "%-8s\t", metaData.getColumnName( i ) );
             System.out.println();
             
             while ( resultSet.next() ) 
             {
                for ( int i = 1; i <= numberOfColumns; i++ )
                   System.out.printf( "%-8s\t", resultSet.getObject( i ) );
                System.out.println();
             } 
          }
          catch ( SQLException sqlException )                                
          {                                                                  
             sqlException.printStackTrace();
          }                                                 
          finally
          {                                                             
             try                                                        
             {                                                          
                resultSet.close();                                      
                statement.close();                                      
                connection.close();                                     
             }                                        
             catch ( Exception exception )                              
             {                                                          
                exception.printStackTrace();                            
             }                                        
          }                                           
       } 
    }
    I am relatively new to Java and I use TextPad 5.4.2 in Windows 7 to build my Java applications.

    For MySQL, I am using WampServer 2.2 which is installed at C:\wamp\bin\mysql\mysql5.5.24\

    To use JDBC connector, I have downloaded the file mysql-connector-java-gpl-5.1.27.msi from MySQL :: Download Connector/J
    I installed this msi file which created a folder named MySQL at C:\Program Files\MySQL\
    Inside it I have a jar file at C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.27-bin.jar

    After successfully compiling my source code above, I get this runtime error message while running:
    java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/books
    ........................................
    I think my problem is, after installing the msi file, now I have two MySQL folders at
    C:\Program Files\MySQL\ and C:\wamp\bin\mysql\mysql5.5.24\. I can't understand how to configure the 'JDBC MySQL connector' with Wamp. Again, I am using TextPad for compiling and running Java, not the Windows command prompt.

    Sorry for too detailed or repetitive description of my problem to make it completely clear. Please help. Thanks.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,004
    Rep Power
    6

    Default Re: MySQL (WampServer) connectivity with Java program using JDBC

    Research the java classpath. You need to put the driver jar on the Java classpath before the code can use it. You can start by googling that error you get ('java mysql no suitable driver found'), you are not the first one to run into this.

    And besides that, you will probably want to add the port that MySQL is listening on to the JDBC URL that you're using. By default that is 3306.

    Java Code:
    jdbc:mysql://localhost:3306/books
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 5
    Last Post: 09-16-2011, 06:58 AM
  2. JDBC Connectivity in netbeans
    By juhiswt in forum New To Java
    Replies: 4
    Last Post: 04-01-2011, 02:04 PM
  3. question about MySQL and java connectivity
    By mr_anderson in forum JDBC
    Replies: 12
    Last Post: 07-21-2010, 04:30 AM
  4. jdbc connectivity error
    By OMFGITSROHIT in forum JDBC
    Replies: 2
    Last Post: 03-16-2010, 05:10 PM
  5. Connectivity issue-MySQL and Java
    By Pragya in forum JDBC
    Replies: 9
    Last Post: 01-27-2010, 05:28 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •