sql server - java.sql.SQLException: No suitable driver found for jdbc:sqlserver: -


problem

dear all, connected java desktop application mssql using jdbc. when try connect java dynamic web application (mssql using jdbc) using same method show exception.

sql exception

java.sql.sqlexception: no suitable driver found jdbc:sqlserver://localhost;databasename=reamsdb;user=sa;password=xxx; 

i try many solutions internet none helped me. kindly me rid of problem.

below files attached.

thanks.

## dbtesting.java ## import java.sql.connection; import java.sql.drivermanager;  import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import java.sql.*; public class dbtesting {         public  void establishconnection() throws sqlexception  {               string user = "sa";             string pass = "ansari12345";                 string connectionurl = "jdbc:sqlserver://localhost;" +                        "databasename=reamsdb;user="+user+";password="+pass+";";             connection con = null;             statement stmt = null;             resultset rs = null;             string sql = "select * basement";              try {                  con = drivermanager.getconnection(connectionurl);                 system.out.println("db connected !");                 } catch (sqlexception e) {                 // todo auto-generated catch block                 e.printstacktrace();                 }             stmt = con.createstatement();             rs = stmt.executequery(sql);             while(rs.next())             {                 int id = rs.getint(1);             string squarefeetcount = rs.getstring(1);             string _finishedpercent = rs.getstring(1);             int s_id = rs.getint(1);               system.out.println("id :"+id+"  squarefeetcount :"+squarefeetcount+" finishedpercent :"+_finishedpercent);              }             con.close();              } 

}

## hello.jsp ## <%@ page language="java" contenttype="text/html; charset=iso-8859-1"     pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>insert title here</title> </head> <body> <form action="jdbcservletpath" method="post"> <input type="submit"> </form> </body> </html> 

i read drivermanager code.drivermanager.getconnection(string url) invoke following code:

code jdk1.7

   private static connection getconnection(         string url, java.util.properties info, class<?> caller) throws sqlexception {         /*          * when callercl null, should check application's          * (which invoking class indirectly)          * classloader, jdbc driver class outside rt.jar          * can loaded here.          */         classloader callercl = caller != null ? caller.getclassloader() : null;         synchronized (drivermanager.class) {             // synchronize loading of correct classloader.             if (callercl == null) {                 callercl = thread.currentthread().getcontextclassloader();             }         }          if(url == null) {             throw new sqlexception("the url cannot null", "08001");         }          println("drivermanager.getconnection(\"" + url + "\")");          // walk through loaded registereddrivers attempting make connection.         // remember first exception gets raised can reraise it.         sqlexception reason = null;          for(driverinfo adriver : registereddrivers) {             // if caller not have permission load driver             // skip it.             if(isdriverallowed(adriver.driver, callercl)) {                 try {                     println("    trying " + adriver.driver.getclass().getname());                     connection con = adriver.driver.connect(url, info);                     if (con != null) {                         // success!                         println("getconnection returning " + adriver.driver.getclass().getname());                         return (con);                     }                 } catch (sqlexception ex) {                     if (reason == null) {                         reason = ex;                     }                 }              } else {                 println("    skipping: " + adriver.getclass().getname());             }          }          // if got here nobody connect.         if (reason != null)    {             println("getconnection failed: " + reason);             throw reason;         }          println("getconnection: no suitable driver found "+ url);         throw new sqlexception("no suitable driver found "+ url, "08001");     } 

if registereddrivers doesn't contain driverinfo of sqlserver driver(or url format error).the code throw sqlexception("no suitable driver found "+ url, "08001").

you can call drivermanager.getdriver(string url) test whether contain dirver.if yes,i think url format error. otherwise,i think lib issue.you can call drivermanager.registerdriver(java.sql.driver driver) or try class.forname before call drivermanager.getconnection. class.forname can check lib. if doesn't have class,it throw classnotfoundexception.

variable registereddrivers initialized @ loadinitialdrivers method.you can @ more useful information.

hope these can you


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -