
Java链接SQL server 一直显示连接失败
packagewe;importjava.sql.*;publicclassjh{publicstaticvoidmain(String[]args){Stringdri...
package we;
import java.sql.*;
public class jh {
public static void main(String[] args) {
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=flight";
String userName="sa"; //默认用户名
String userPwd="12345"; //密码
Connection con = null; //数据库连接对象
try{
if (con == null)
{ //如果连接对象为空
Class.forName(driverName);
Connection dbconn=DriverManager.getConnection(dbURL,userName,userPwd);
}
System.out.println("连接成功!"); //如果连接成功,控制台输出“连接成功!”
}catch(Exception e){
System.out.println("连接失败!");
}
}
} 展开
import java.sql.*;
public class jh {
public static void main(String[] args) {
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=flight";
String userName="sa"; //默认用户名
String userPwd="12345"; //密码
Connection con = null; //数据库连接对象
try{
if (con == null)
{ //如果连接对象为空
Class.forName(driverName);
Connection dbconn=DriverManager.getConnection(dbURL,userName,userPwd);
}
System.out.println("连接成功!"); //如果连接成功,控制台输出“连接成功!”
}catch(Exception e){
System.out.println("连接失败!");
}
}
} 展开
2个回答
展开全部
1 连数据库的jar包没加入,
2 数据库账号密码错误
你把Exception的具体错误打印出来
System.out.println("连接失败:" + e.getMessage());
2 数据库账号密码错误
你把Exception的具体错误打印出来
System.out.println("连接失败:" + e.getMessage());
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询