java找不到sql驱动程序,java.sql.SQLException:找不到适合jdbc:sqlserver的驱动程序

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.

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值