连接驱动已经导入到项目内,但是仍然提示错误
错误提示:
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testDB62
代码:
package JDBC;
import static org.junit.Assert.*;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import org.junit.Test;
public class JDBCTest {
@Test
public void testDriverManager() throws Exception{
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
//读取类路径下的 jdbc.properties 文件
InputStream in =
getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
//2. 加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块.)
Class.forName(driverClass);
//3. 通过 DriverManager 的 getConnection() 方法获取数据库连接.
Connection connection =
DriverManager.getConnection(jdbcUrl,user,password);
System.out.println(connection);
}
}
jdbc.properties:
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbcUrl=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testDB62
user=sa
password=*****