【问题标题】:Cant connect to SQLSERVER using Eclipse IDE无法使用 Eclipse IDE 连接到 SQL SERVER
【发布时间】:2020-02-21 00:00:35
【问题描述】:

我是一名学生,我正在编写以下代码。我一直在寻找自己的解决方案,但找不到任何解决方案。

代码如下:

import java.sql.Connection;
import java.sql.DriverManager;


public class ConexionSQL {


        private static Connection cn;

        public static Connection getConnection() {
            try {

                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                cn = DriverManager.getConnection("jdbc:sqlserver://Frankcpu:1433;database=Dentista;user=sa;password=123;encrypt=true;trustServerCertificate=false;loginTimeout=30;");

            }catch(Exception e) {
                cn= null;

            }
            return cn;
        }


    public static void main(String[] args) {

        Connection pruebaCn = ConexionSQL.getConnection();
    if(pruebaCn!=null) {
        System.out.println("Conectado");
        System.out.println(pruebaCn);
    }else {
        System.out.println("No Conectado");
    }
    }

}

【问题讨论】:

  • 如果您告诉我们您遇到了什么错误,您是否检查了连接是否与另一个程序一起工作等等,即您已经做过的事情,答案将会快很多尝试诊断问题。
  • 另外,不要捕获Exception 而不是特定异常。是什么例外?它说了什么?
  • 请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
  • 没有错误,它只是运行,我在控制台中得到“未连接”

标签: java sql-server eclipse


【解决方案1】:

您的代码的第一部分似乎没问题。那你可以试试这个:

String dbURL = "jdbc:sqlserver://localhost\\sqlexpress;user=sa;password=secret";
Connection conn = DriverManager.getConnection(dbURL);

或者你也可以使用 Properties 类:

String dbURL = "jdbc:sqlserver://localhost\\sqlexpress";
Properties properties = new Properties();
properties.put("user", "sa");
properties.put("password", "secret");
conn = DriverManager.getConnection(dbURL, properties);

检查您的 JDBC 是否正确连接到 eclipse,并检查类路径是否正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-03
    • 2020-05-22
    • 2015-08-29
    • 2016-11-08
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多