【问题标题】:Microsoft Sql Server not fetching the records with Where Condition using javaMicrosoft Sql Server 未使用 java 获取具有 Where 条件的记录
【发布时间】:2018-11-05 10:25:16
【问题描述】:

我可以使用Select * from table_name; 的简单查询打印所有记录,但是当我使用Where 子句时,它没有找到任何行,在我的表列索引是 7,请帮助我如何解决这个问题。

select * from TABLE_NAME (nolock) where CUSTOMERID=765432345;

请提供原因,为什么会发生这种情况,但我尝试使用另一个查询从客户表中获取客户 ID,然后它可以正常工作,就像下面的代码一样。

Java 代码是:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;    

public class SqlServerDB {

    public static final String connectionUrl = "jdbc:sqlserver://ip:PORT;DatabaseName=<db name>";
    public static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    static String userName = "xxxxxxxxx";
    static String password = "xxxxxxxxx";
    public static Connection conn;
    static ResultSet query;
    static String status;
    static Statement statement;

    public static Connection connect() {

        System.out.println("Sql Server Database Connection Started");
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(connectionUrl, userName, password);
            if (conn != null) {
                System.out.println("Database Connection is established");
            } else {
                System.out.println("Database is not Connected");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }

    public static void main(String args[]) throws SQLException {
        conn = connect();
        // Create Statement
        statement = conn.createStatement();
        String sqlQ = "select * from TABLE_NAME (nolock) where CUSTOMERID=765432345";
        // Execute the query
        query = statement.executeQuery(sqlQ);

        if(query.next()) {
            if(query.getString("INVOICE")!=null) {
                System.out.println(status=query.getString("INVOICE"));  
            }
        } else {
            System.out.println("No rows Returned");
        }
    }
}

【问题讨论】:

  • 表中有没有CUSTOMERID=765432345的记录?
  • 是的,仅限单条记录。
  • 尝试关闭连接到底conn.close()
  • 表格中是否存在 INVOICE 列?
  • 客户#765432345的发票列是否包含NULL?

标签: java sql-server


【解决方案1】:

可能区分大小写?

试试

select * from table_name (nolock) where CUSTOMERID=765432345;

【讨论】:

  • 还有 select * from table_name (nolock) where customerid=765432345; ?
  • CUSTOMERID 是否定义为字符串?
猜你喜欢
  • 1970-01-01
  • 2011-10-21
  • 2019-06-03
  • 1970-01-01
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 2013-07-18
  • 1970-01-01
相关资源
最近更新 更多