Java PreparedStatement未设置值

时间:2019-03-12 10:47:51

标签: java sql-server prepared-statement

我对Java完全陌生,我尝试进行一些测试以从MSSQL数据库读取一些数据。我必须将一些值传递给查询,但这不能正常工作,如果我手动设置它们,则它可以工作,在我的情况下,使用PreparedStatement和.setLong-Method则无法工作。

public class db_testClass {
public static void main(String[] args) throws ClassNotFoundException {

    long firstId = 0;
    long lastId = 201801001010010403L;
    PreparedStatement statement;
    int counter = 1;

     String SQL = "IF OBJECT_ID('tempdb..#tmpDaten') IS NOT NULL
                "            DROP TABLE #tmpDaten;  
                "        SELECT  DISTINCT 
                "                RIGHT(10000000 + ISNULL(r.xxx, 0), 7) AS Wert
                "        INTO    #tmpDaten 
                "        FROM    dbo.xxx  
                "        WHERE   r.xxxx BETWEEN firstId = ? AND lastId = ?; 

                "        SELECT DISTINCT 
                "                'xxxx' AS Art ,
                "                t.xxx 
                "        FROM    #tmpDaten 
                "                LEFT JOIN xxxxxxx a ON a.xxxx = t.xxxx 
                "                AND a.ART =   'xxxx'                                     " + 
                "        WHERE   a.xxxx IS NULL;";


    String connectionUrl = "jdbc:sqlserver://xxxxxx:1433;databaseName=xxxxxx;integratedSecurity=true;";
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    try {

            Connection con = DriverManager.getConnection(connectionUrl); 
            statement = con.prepareStatement(SQL);
            statement.setLong(1, firstId);
            statement.setLong(2, lastId);
            System.out.println(statement);
            ResultSet rs = statement.executeQuery();


    }
    catch (SQLException e) {
        e.printStackTrace();
    }
  }
}

错误表明'='附近有错误的语法......

有任何想法吗?

谢谢

丹尼尔

0 个答案:

没有答案