1

I am working on a web application where I am creating MSSQLSERVER 2008 database dynamically.

But it's giving me

java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost:1433;databaseName=master

My code is:

String dbName = "db1";
try {
    String url = "jdbc:sqlserver://localhost:1433;databaseName=master";
    Connection connection = DriverManager.getConnection(
                 url,
                 "sa",
                 "roshan14121987");

    Statement statement = connection.createStatement();
    String sqlquery = "CREATE Database \"" + dbName + "\"; ";
    statement.executeUpdate(sqlquery);
    statement.close();

    connection.close();

} catch(Exception e) {
    e.printStackTrace(); 
}

I have added sqljdbc4.jar in lib. I have tried it on both NetBeans (with GlassFish and Tomcat Server) and Eclipse IDE(Tomcat Server).

On the other hand I have tried this with simple desktop application, it's working fine on both IDEs. With sqljdbc4.jar added in lib.

Elrond_EGLDer
  • 47,430
  • 25
  • 189
  • 180
Aniket
  • 2,044
  • 4
  • 29
  • 48

3 Answers3

7

Before calling DriverManager.getConnection() you will have to load the SQLServer JDBC driver. You can do Class.forName("xxxx"); where xxxx is the appropriate driver class (fully qualified name with package prefix).

EDIT: Do this Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); to load the driver. Refer MSDN link for more.

kenorb
  • 118,428
  • 63
  • 588
  • 624
Bimalesh Jha
  • 1,402
  • 9
  • 13
  • +1 Nice catch, this could be it! – acdcjunior Jul 20 '13 at 08:06
  • @Bimalesh yes it working with Class.forName("xxxx"), but i confused why it is working without Class.forName("xxxx") when i used this same code in simple java class. – Aniket Jul 20 '13 at 08:53
  • @Aniket your code must be referring/loading the driver somewhere, not necessarily in your code, but may be some initialization routine somewhere else. Pls dig deeper. – Bimalesh Jha Jul 20 '13 at 17:52
  • In my experience the Class.forName call was not needed for stand-alone Java applications, but I needed to add it when I used the same code in a Tomcat web application. – Bampfer May 12 '15 at 21:37
  • I find a strange behavior with my application. I could connect to SQL without Class.for..... line but when I run it as a Jar I could not connect to SQL. What must have happened? – manikanta nvsr Jun 09 '20 at 11:26
  • 1
    @manikantanvsr that is due to changes in modern versions of java runtime using service providers mechanism https://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#Service%20Provider . Please check this answer https://stackoverflow.com/questions/28220724/class-fornamejdbc-driver-no-longer-needed You can implement a services/META-INF for this in your jar package. – Bimalesh Jha Jun 10 '20 at 15:53
2

Is sqljdbc4.jar in your war file under WEB-INF/lib. This is the alternate location to $CATALINA_HOME/lib and recommended to keep your web application self-contained. Further, if you should ever need to change servers, you need only drop the war into your webapps directory.

You might also need a Class.forName([database class]).newInstance, but that isn't necessary using a JDBC 4.0 driver. Good luck and if you have further problems, do leave a comment.

Community
  • 1
  • 1
hd1
  • 30,506
  • 4
  • 69
  • 81
1

The error happen in the runtime on Tomcat Server,right? I think you need to make sure your sqljdbc4.jar deploy the in the Server.

Martin
  • 71
  • 1
  • 1
  • 4