加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JDBC的JDBC示例

发布时间:2020-12-14 05:48:01 所属栏目:Java 来源:网络整理
导读:我已经下载了JDK 6,我也有sqljdb4.jar,我有 database.properties文件,其中包含以下数据 database.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriverdatabase.url=jdbc:sqlserver://.;databaseName=UserInfo;integratedSecurity=true; database.
我已经下载了JDK 6,我也有sqljdb4.jar,我有 database.properties文件,其中包含以下数据
database.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
database.url=jdbc:sqlserver://.;databaseName=UserInfo;integratedSecurity=true; 
database.username=sa
database.password=admin

B.N:我正在我的机器上安装服务器,服务器名称=.,我也在使用Windows Authontication

我现在的问题是当我尝试创建连接时出现以下错误

com.microsoft.sqlserver.jdbc.SQLServerException:
The TCP/IP connection to the host
localhost,port 1433 has failed.
Error: Connection refused: connect.
Please verify the connection
properties and check that a SQL Server
instance is running on the host and
accepting TCP/IP connections at the
port,and that no firewall is blocking
TCP connections to the port. at
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)

我不知道这里的确切问题是什么

如果任何人可以帮助我将不胜感激

提前致谢

解决方法

这是由许多概率引起的
1- IP是worong
2-端口错误
3-防火墙防止机器外出并连接到另一个IP
4- SQL服务器关闭.

试着用

public class JdbcSQLServerDriverUrlExample
{
  public static void main(String[] args)
  {
    Connection connection = null;
    try
    {
      // the sql server driver string
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

      // the sql server url
      String url = "jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE";

      // get the sql server database connection
      connection = DriverManager.getConnection(url,"THE_USER","THE_PASSWORD");

      // now do whatever you want to do with the connection
      // ...

    }
    catch (ClassNotFoundException e)
    {
      e.printStackTrace();
      System.exit(1);
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      System.exit(2);
    }
  }
}

我需要解释的是,有一种非常好的技术称为“持久性”,它比JDBC更好,并且非常出色且易于使用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读