java连接SQL SERVER失败问题,给为帮忙看一下,谢谢了!
程序代码:
//SourceCode1 package SqlTest; import java.awt.dnd.DnDConstants; import java.sql.*; import java.util.function.DoubleConsumer; public class ConectTest { public static void main(String [] args) { String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=SPJ"; String userName="sa"; String userPwd="zhao1234"; // Statement stmt=null; PreparedStatement pstmt = null; ResultSet rs=null; Connection dbCon=null; try { Class.forName(driverName); System.out.println("加载驱动成功!"); }catch(Exception e){ e.printStackTrace(); System.out.println("加载驱动失败!"); } try{ dbCon=DriverManager.getConnection(dbURL,userName,userPwd); System.out.println("连接数据库成功!"); }catch(Exception e) { e.printStackTrace(); System.out.print("SQL Server连接失败!"); }
//SourceCode2
package Shopping;
import java.sql.*;
public class Conn {
public static Connection getSqlConnect() {
Connection Con = null;
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=SPJ";
String userName = "sa";
String userPwd = "zhao1234";
try {
Class.forName(driverName);
System.out.println("加载驱动成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("加载驱动失败!");
}
try {
Con = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("连接数据库成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.print("SQL Server连接失败!");
}
return Con;
}
// 测试数据库连接
public static void main(String[] args) {
Conn aConn=new Conn();
aConn.getSqlConnect();
}
}
同样的配置和运行环境,两个文件,第一个连接通过,第二个就显示失败,是代码的问题吗?我不是很了解java。或者是其他的什么问题吗?各位大佬,帮忙看下好吗?谢谢大家了!