-3

I'm making a small program in Java that uses a Mysql connection but im getting some problems with the jdbc drivers. I installed Java EE and Java SE but i still get the message that there are no suitable driver for jdbc:mysql://localhost:3307/test. Can someone explain to me what i am doing wrong.

Code:

public class Mysql_Connection_2 {


    /**
     * @param args the command line arguments
     */
    static String query = "select count(*) from stock";    

    public static void main(String[] args) {

        try {           
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
        }
        catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(MysqlConnection.class.getName()).log(Level.SEVERE, null, ex);
        }          
        MysqlConnection.dbConnection(query);
    }  
}

Extern Connection class:

public class MysqlConnection {

    private static final String dbURL = "jdbc:mysql://localhost:3307/test";
    private static final String dbuname = "root";
    private static final String dbpass = "usbw";

    static Connection dbcon = null;
    static Statement stmt = null;
    static ResultSet rs = null;



    public static void dbConnection (String query){

        try{         
            //getting database connection to MySQL server
            dbcon = DriverManager.getConnection(dbURL, dbuname, dbpass);

            //getting PreparedStatment to execute query
            stmt = dbcon.prepareStatement(query);

            //Resultset returned by query
            rs = stmt.executeQuery(query);

            while(rs.next()) {
                int count = rs.getInt(1);
                System.out.println("count of stock : " + count);
            }
        }
        catch(SQLException ex){
           System.out.println(ex.getMessage());           
        }       
    }   

}
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
The_Monster
  • 494
  • 2
  • 7
  • 28

6 Answers6

2
  1. First You need to correct your driver

    Class.forName("com.mysql.jdbc.Driver");

  2. Then Check You added mysql connector jar file

  3. after that you need to make sure that jar file is added to your class path or not.

Correct the First one and go to second and then third

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
Lasitha Benaragama
  • 2,201
  • 2
  • 27
  • 43
  • I think the order should be 2,3 then 1. The first step for anyone would do is to make sure they have the jar. Then they will add to the class path before going hard on keyboard. – NewUser May 20 '14 at 08:38
  • Actually 1 is the basic Error. You have to correct it first right. Then still database is not connecting then go to 2 and 3 thats what i thought. – Lasitha Benaragama May 20 '14 at 08:40
  • 1
    `Class.forName("com.mysql.jdbc.Driver");` hasn't been necessary since Java 6 / JDBC 4.0 as long as you use a JDBC 4.0 driver. – Mark Rotteveel May 20 '14 at 09:47
1

I can see one problem here. You are using Oracle driver to perform operation on the MySQL database.

try {           
    Class.forName("com.mysql.jdbc.Driver");
}          

Try this.

You need not specify the port number if you are using the default port.jdbc:mysql://localhost/dbName should do

NewUser
  • 3,729
  • 10
  • 57
  • 79
1

If you are trying to use mysql then the class should be com.mysql.jdbc.Driver and you should have mysql jdbc connectivity jar file in class path.

Initialize the driver using

Class.forName("com.mysql.jdbc.Driver").newInstance();
Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
1

you are trying to access mysql database through oracle driver so you are getting error

try to use

Class.forName("com.mysql.jdbc.Driver");
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
1

try this

try {           
Class.forName("com.mysql.jdbc.Driver");
}     

and

private static final String dbURL = "jdbc:mysql://localhost:3306/test";
Gabber
  • 7,169
  • 3
  • 32
  • 46
1

1) As pointed out in above answers, for MySql you should use

Class.forName("com.mysql.jdbc.Driver");

2) Make sure you have jar in classpath. You can download jar from mvnrepo MVNREPO

3) Port is 3306 for MySql?

And a quick google for JAVA + MYSQL gives me this tutorial

Shashank Kulkarni
  • 100
  • 1
  • 3
  • 11
  • `Class.forName("com.mysql.jdbc.Driver");` hasn't been necessary since Java 6 / JDBC 4.0 as long as you use a JDBC 4.0 driver. – Mark Rotteveel May 20 '14 at 09:47