日期:2014-05-18 浏览次数:20746 次
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JDBCConn { public static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; public static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=books"; public static final String USER = "sa"; public static final String PWD = "ok"; private Connection conn = null; public Connection getConn() { try { Class.forName(DRIVER); conn = DriverManager.getConnection(URL,USER,PWD); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } }
------解决方案--------------------
你的数据库端口配置自己改成8080了吗,8080一般是给mysql预留的端口?SQLServer应该是1443吧!
------解决方案--------------------
打开配置工具SQL server Configuration Manager,SQLEXPRESS协议中启动tic/ip.
双击打开tip/ip属性。ipAll部分把tcp端口设置为1433.重新启动电脑
------解决方案--------------------
url写的有问题
应该是
url = “jdbc:microsoft:sqlserver://localhost:1433;databaseName=数据库名”;