简体   繁体   English

无法通过我的选择查询从SQL Server获取数据(使用jdbc驱动程序)

[英]Can't get data from a SQL Server with my select query (using jdbc driver)

I am trying to connect to a SQL Server 2008 R2 instance which is using sql auth. 我正在尝试连接到使用sql身份验证的SQL Server 2008 R2实例。 No exception is raised (I'm catching SqlException), and my code is as follows: 没有引发异常(我正在捕获SqlException),我的代码如下:

                    SQLServerDataSource ds = new SQLServerDataSource();
        ds.setUser(dbUserID);
        ds.setPassword(Password);
        ds.setServerName(DBServer);
        ds.setPortNumber(1433);

        ds.setDatabaseName(dbName);

        con = (SQLServerConnection) ds.getConnection();


        Statement statement = con.createStatement();
                     resultSet = statement.executeQuery(SQL);

All the parameters (username, password, etc) are 100% correct. 所有参数(用户名,密码等)均为100%正确。 The database is running etc. The query is a simple select * from a table which exists. 数据库正在运行等。查询是从存在的表中简单选择*。 There are no null objects after they're used, connection object is setup etc, but the resultset has 0 rows. 使用完后没有空对象,没有设置连接对象,但是结果集有0行。

Any ideas what I'm doing wrong? 有什么想法我做错了吗? I'm using Microsoft SQL Server JDBC Driver 3.0 . 我正在使用Microsoft SQL Server JDBC Driver 3.0

Thanks 谢谢

Making some assumptions here: 在这里做一些假设:

  1. You are using SQL Auth and you can connect using the dbuserid and password parameters from either the command line or even SQL Management Studio. 您正在使用SQL Auth,并且可以从命令行甚至是SQL Management Studio使用dbuserid和password参数进行连接。

  2. You can telnet into the server on 1433, ie make sure there is not firewall or port forwarding issues. 您可以在1433上telnet到服务器,即确保没有防火墙或端口转发问题。 Use telnet 123.123.123.123 1433. You should make a connection right away. 使用telnet 123.123.123.1231433。您应该立即建立连接。 If not, you need to fix that. 如果不是,则需要修复该问题。

I took you code and just ran it against one of my SQL 2008 databases using JDBC 3 (excuse the quick and dirty code). 我拿了您的代码,然后使用JDBC 3在我的SQL 2008数据库之一中运行了该代码(请使用快速而肮脏的代码)。

import com.microsoft.sqlserver.jdbc.SQLServerConnection;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;

import java.sql.ResultSet;
import java.sql.Statement;

public class testdelete
{
   private static void displayRow(String title, ResultSet rs) {
      try {
         System.out.println(title);
         System.out.println("Sales ID - Pkts Sold");
         while (rs.next()) {
            System.out.println(rs.getString("SALES_ID") + " : " + rs.getString("Pkts"));
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

    public static void main(String[] args)
    {
        System.out.println("testing...");

        String dbUserID = "sa";
        String Password = "MYPWD";
        String DBServer = "123.123.123.123";
        String dbName = "MYDB";

        SQLServerConnection con;
        ResultSet rs;

        String SQL = "select top 10 * from Sales";

        SQLServerDataSource ds = new SQLServerDataSource();
        ds.setUser(dbUserID);
        ds.setPassword(Password);
        ds.setServerName(DBServer);
        ds.setPortNumber(1433);

        ds.setDatabaseName(dbName);

        try
        {
            con = (SQLServerConnection) ds.getConnection();
            Statement statement = con.createStatement();
            rs = statement.executeQuery(SQL);
            displayRow("SALES", rs);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们可以在 Java 中使用 JDBC 驱动程序使用 Select 查询来获取顶点数据吗 - Can we use the Select query to get the vertex data using JDBC Driver in Java SQL Server JDBC驱动程序无法快速检测丢失的连接 - SQL Server JDBC driver can't detect lost connection quickly 无法使用 JDBC 驱动程序 SQLState 08001 连接到 MS SQL Server - Can not connect to MS SQL Server using JDBC driver SQLState 08001 无法使用JDBC连接到SQL Server - Can't connect to SQL Server using JDBC 无法使用 JDBC 驱动程序连接到 Salesforce - Can't get connection to Salesforce using JDBC driver SQL查询返回“ oracle.jdbc.driver.OracleResultSetImpl@48f675”而不是我的预期数据 - SQL query returning “oracle.jdbc.driver.OracleResultSetImpl@48f675” instead of my expected data 无法使用 MS JDBC 驱动程序从 CentOS 8 服务器连接到 SQL 服务器服务器:使用安全连接 ZEA52C2Z42253C5F99C23Z 错误 - Cannot connect to SQL Server server with MS JDBC Driver from CentOS 8 server: SSL error but not using secure connection 在SQL Server上使用jdbc PreparedStatement获取查询计划 - Get the query plan using jdbc PreparedStatement on sql server 是否可以在sql server上使用jdbc获取查询计划? - is it possible to get the query plan out using jdbc on sql server? 无法使用JDBC驱动程序和sql2o连接到我的Posgres服务器 - Unable to connect to my Posgres server using JDBC driver and sql2o
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM