0

I have a simple code that needs to connect to a mysql DB and execute a query.

try {
            conn =
               DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=1234");

                Statement stm = conn.createStatement();
                stm.executeQuery(query);
                return true;

This code works like a charm in a test project with only a class with void main. But in my project that is a web application, I'm using apache tomcat and the same code displays the error:

SQLException: No suitable driver found for jdbc:mysql://localhost/test?user=root&password=1234

I've added the jar, the classpath the everything. Is there something special that I need to do because I'm using tomcat? Thanks, Dragos

Mise
  • 3,267
  • 1
  • 22
  • 22
Dragos
  • 296
  • 2
  • 4
  • 13
  • possible duplicte of : http://stackoverflow.com/questions/22384710/java-sql-sqlexception-no-suitable-driver-found-for-jdbcmysql-localhost3306 http://stackoverflow.com/questions/22384710/java-sql-sqlexception-no-suitable-driver-found-for-jdbcmysql-localhost3306 – nonamer92 Oct 26 '14 at 17:15
  • you forgot to put the `port` no: `3306` replace the line as this `jdbc:mysql://localhost:3306/dbname?user=root&password=1234` – Vighanesh Gursale Oct 26 '14 at 17:15
  • Did that and still the same thing. – Dragos Oct 26 '14 at 17:16

3 Answers3

1

Use:

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=1234");
Mise
  • 3,267
  • 1
  • 22
  • 22
  • 1
    `Class.forName()` is no longer necessary. Since Java 6 a proper driver will register itself automatically. –  Oct 26 '14 at 17:19
  • I admit this is probably the answer. I would be careful with this answer because there are scenarios where you wouldn't be able to load the driver even though you tried to load the class in this fashion. In other words, this answer works for "most" instances but when we are talking about a Tomcat container being involved, your introducing more variables into the question. Java doesn't have namespaces, but you could have a similar issue. If Dragos doesn't mark this answer green, then we will know there was a problem. – djangofan Oct 26 '14 at 17:20
  • @a_horse_with_no_name I think you mean JDBC4 autoloads... its not a Java 6+ thing. – djangofan Oct 26 '14 at 17:23
  • @djangofan: JDBC 4.0 was introduced with Java6 - so it is a Java6 thing (you can't have JDBC4 with Java 5). And I think the service registration from Java6 is not limited to JDBC drivers - the JDBC API just makes use of that (but I could be mistaken there) –  Oct 26 '14 at 17:24
  • @a_horse_with_no_name What I mean was that you could use JDBC3 with Java 6 and it would not autoload. – djangofan Oct 26 '14 at 17:28
  • I'm using tomcat8 and struts2. is that something to consider? – Dragos Oct 26 '14 at 17:34
1

I have found the problem. I had to add the jdbc in the deployment assembly above the java build path in the configure build path window.

Dragos
  • 296
  • 2
  • 4
  • 13
0

You forgot to load driver

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

Before getting connection. Also you are missing port from your url.

Also you should be using

getConnection(String url, String user, String password) 

API for better readability rather than providing user/pass in url.

SMA
  • 36,381
  • 8
  • 49
  • 73
  • `Class.forName()` is no longer necessary. Since Java 6 a proper driver will register itself automatically. –  Oct 26 '14 at 17:18
  • How do you know OP is using Java 6? – SMA Oct 26 '14 at 17:19
  • Now I get SEVERE: Exception occurred during processing request: com.mysql.jdbc.Driver java.lang.ClassNotFoundException: com.mysql.jdbc.Driver – Dragos Oct 26 '14 at 17:20
  • I just wanna make the connection work. After I can make it into proper code. – Dragos Oct 26 '14 at 17:21
  • So you dont have driver in your classpath. Make sure you have mysql driver in your classpath. See this about driver url http://www.vogella.com/tutorials/MySQLJava/article.html#jdbcdriver and steps you need to follow – SMA Oct 26 '14 at 17:21
  • It is added in the configure build path – Dragos Oct 26 '14 at 17:24
  • Is it important to mention that I use jsp and struts2? – Dragos Oct 26 '14 at 17:32