Microsoft SQL Server无法使用Java使用Where Condition获取记录

时间:2018-11-05 10:25:17

标签: java sql-server

我可以通过简单查询将所有记录打印为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");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

也许区分大小写?

尝试

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