- Java code
import java.sql.*;public class test { public static void main(String[] args) { String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL = "jdbc:microsoft:sqlserver://localhost:183.188.198.16:1433;DatabaseName=database_demo"; String userName = "sa"; // 默认用户名 String userPwd = "123456"; // 密码 try { Class.forName(driverName); Connection dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("连接成功!"); // 如果连接成功,控制台输出“连接成功!” } catch (Exception e) { //System.out.println("连接失败!"); e.printStackTrace(); } }}
以下是错误
- Java code
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:183.188.198.16:1433;DatabaseName=database_demo at java.sql.DriverManager.getConnection(DriverManager.java:604) at java.sql.DriverManager.getConnection(DriverManager.java:221) at test.main(test.java:11)
------解决方案--------------------
看了他的JDBC驱动包,将url地址改了,还有他自己连接数据库就不存在,换了一个数据库
还有出现密码不对的情况
- Java code
import java.sql.DriverManager;import java.sql.Connection;public class TestSQL { public static void main(String[] args) { String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=database_demo"; String userName = "sa"; // 默认用户名 String userPwd = "123456"; // 密码 try { Class.forName(driverName); Connection dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("连接成功!"); // 如果连接成功,控制台输出“连接成功!” } catch (Exception e) { //System.out.println("连接失败!"); e.printStackTrace(); } }}