1
votes

I have compiled my code using -cp <mysql-connector-java jar file>. There were no errors during the compilation. All the class files were created

To run the code, I went to one directory higher and did java packagename.MyPgm.
However, when i run this is the error that pops up:

Error in line no: -1
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/feedback?user=root&password=passwd
    at java.sql.DriverManager.getConnection(DriverManager.java:640)
    at java.sql.DriverManager.getConnection(DriverManager.java:222)
    at javaapplication2.HttpHeaderParser.main(HttpHeaderParser.java:69)


import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.io.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;

public class HttpHeaderParser {

static Connection connect = null;
static Statement statement = null;
public static void main(String[] args) throws IOException {
    try {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("http_headers.txt")));   
        FirstLine firstObj = null;
        SecondLine secondObj;
        OtherFields otherObj;
        connect = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/feedback?"+"user=root&password=sicily");  //this will connect to the database
        statement = (Statement) connect.createStatement();

I've got some java String operations here. And then the catch statement.

catch (Exception e) {
        System.out.println("Error in line no: "+lineCount);                 //displays the line number of the input file where the program stopped
        e.printStackTrace();
    }
2
The program is syntactically correct. The problem arises during run-time. The DriverManager is not able to get or find the instance during run time.Maximin
Is this the complete stack trace that you are getting?Maximin
Yes, this is all i'm getting.Dhruva Jain
Edit your question by including the code that you used.Maximin
There you go. Please have a look.Dhruva Jain

2 Answers

1
votes

You need a file something like "mysql-connector-java-5.1.17-bin.jar" in your classpath, and the appropriate references to it in your program

It appears you are replacing the classpath to reference only the one file, You probably need at least "." also.

1
votes

You still need to run the code with classpath argument.

So should be:

java packagename.MyPgm -cp <mysql-connector-java jar file>

Try it out and let/

EDIT:

This line is supposed to be added, before creating the connection.

sample:

Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username",  "password");
conn.close();

EDIT 2:

Fixing only certain part of code:

OtherFields otherObj;
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/feedback?"+"user=root&password=sicily");  //this will connect to the database
statement = (Statement) connect.createStatement();

Once this is done, compile with,

javac HttpHeaderParser.java -cp <mysql jar file>

When compilation is successful, you need to run the same.

java HttpHeaderParser -cp <mysql jar file>

But remember the place where you are writing the JDBC code does not look like a right place.