简体   繁体   中英

MySQL JDBC error on connection

I am trying to establish a connection to remote mysql db. I have put the jar connector to war/WEB-INF/lib folder and added a library (build path).

Still I am getting this error:

java.sql.SQLException: No suitable driver found for jdbc:sql4.freemysqlhosting.net
    at java.sql.DriverManager.getConnection(Unknown Source)

Here is the full code:

/**
 * 
 */
package com.infograph.dbconnection;

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

/**
 * @author Vlad
 *
 */
public class DBConnection 
{
    /**
     * 
     */
    public DBConnection() 
    {
        try 
        {
            Class.forName("com.mysql.jdbc.Driver");
        } 
        catch (ClassNotFoundException e) 
        {
            System.out.println("Where is your MySQL JDBC Driver?");
            e.printStackTrace();
            return;
        }

        System.out.println("MySQL JDBC Driver Registered!");
        Connection connection = null;

        try 
        {
            connection = DriverManager.getConnection("jdbc:sql4.freemysqlhosting.net","user", "pass");

        } 
        catch (SQLException e1) 
        {
            System.out.println("Connection Failed! Check output console");
            e1.printStackTrace();
            return;
        }

        if (connection != null) 
        {
            System.out.println("You made it, take control your database now!");
        } 
        else 
        {
            System.out.println("Failed to make connection!");
        }
    }
}

What is the problem?? 10x!

I think what you meant to use is

connection = DriverManager.getConnection("jdbc:mysql://sql4.freemysqlhosting.net","user", "pass");

You possibly need to add a port number and schema name.

The URL you provided, jdbc:sql4.freemysqlhosting.net , is not valid, or at least it doesn't seem like it.

Your connection url must be in form jdbc:mysql://hostname:port/schema in order for DriverManager to determine which driver to use. Here's more on this: http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM