java.sql.SQLException:找不到适用于jdbc:sqlserver的驱动程序 [英] java.sql.SQLException: No suitable driver found for jdbc:sqlserver
问题描述
我正在创建MSSQLSERVER 2008的Web应用程序上工作 动态数据库.
I am working on a web application where I am creating MSSQLSERVER 2008 database dynamically.
但这给了我
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost:1433;databaseName=master
我的代码是:
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();
}
我在lib中添加了sqljdbc4.jar.我已经在NetBeans(带有GlassFish和Tomcat服务器)和Eclipse IDE(Tomcat服务器)上进行了尝试.
I have added sqljdbc4.jar in lib. I have tried it on both NetBeans (with GlassFish and Tomcat Server) and Eclipse IDE(Tomcat Server).
另一方面,我已经在简单的桌面应用程序上进行过尝试,在两个IDE上都可以正常工作.在库中添加了sqljdbc4.jar.
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.
推荐答案
在调用DriverManager.getConnection()
之前,您必须加载SQLServer JDBC驱动程序.您可以执行Class.forName("xxxx");
,其中xxxx是适当的驱动程序类(带有程序包前缀的完全限定名称).
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).
执行此Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
加载驱动程序.有关更多信息,请参考 MSDN链接.
Do this Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
to load the driver. Refer MSDN link for more.
这篇关于java.sql.SQLException:找不到适用于jdbc:sqlserver的驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!