2014-02-14 50 views
1

我有一些正在向數據庫發送查詢的類。當查詢是在代碼中時,一切工作正常,但由於它很大,我決定把它放在一個文件中,並用緩衝讀取器讀取它,但它不工作,我總是得到這個:如何從Java中的txt文件讀取sql查詢

值java.sql.SQLException:ORA-00900:無效的SQL語句

這裏是我的查詢:

SELECT 
      p.first_name   \"FirstName\", 
      p.middle_name   \"MiddleName\", 
      p.last_name   \"LastName\", 
      p.birth_name   \"BirthName\", 
      p.mothers_name   \"MothersName\", 
      p.nick_name   \"NickName\", 
      p.username    \"Username\", 
      p.currency_id   \"Currency\", 
      p.preferred_language_id \"PreferredLanguage\", 
      p.accepts_email  \"AcceptsEmail\", 
      p.birth_date   \"BirthDate\", 
      p.hear_about_us  \"HeardAboutUs\", 
      p.tax_category_id  \"TaxCategory\", 
      p.birth_place   \"BirthPlace\", 
      p.accepts_id_verification  \"AcceptsIdentityVerification\", 
      p.security_prompt  \"SecurityPrompt\", 
      p.gender_id   \"Gender\", 
      p.tracking_campaign_name \"TrackingCampaign\", 
      p.accepts_sms   \"AcceptsSMS\", 
      p.accepts_promotional_sms \"AcceptsPromotionalSMS\", 
      p.identification_number \"IdentificationNumber\", 
      p.id_verified_id  \"IdentificationVerified\", 
      p.security_word  \"SecurityWord\", 
      p.ident_manual_verified_until \"IdentificationManualVerified\", 
      p.accepts_chat   \"AcceptsChat\", 
      p.frequent_player_level_id  \"FrequentPlayerLevel\", 
      p.preferred_comm_channel \"PreferredCommunicationChannel\", 
      p.is_reward_abuser   \"IsRewardAbuser\", 
      p.newsletter_id  \"Newsletter\", 
      p.accepts_rewards  \"AcceptsRewards\", 
      ci.postal_code   \"PostalCode\", 
      ci.country_id   \"Country\", 
      ci.region   \"Region\", 
      ci.email   \"Email\", 
      ci.address1   \"Address1\", 
      ci.address2   \"Address2\", 
      ci.address3   \"Address3\", 
      ci.phone1   \"Phone1\", 
      ci.phone2   \"Phone2\", 
      ci.city   \"City\", 
      ci.mobile_phone  \"MobilePhone\", 
      ci.address_state_id  \"AddressVerified\" 

    FROM 
      player p 
      JOIN contact_info ci  ON p.CONTACT_INFO_ID = ci.CONTACT_INFO_ID 
      JOIN player_session ps ON p.PLAYER_ID = ps.PLAYER_ID 
    WHERE 
      ps.external_client_session_id = \'$sessionID\' 

這裏是我使用的代碼:

String query = ""; 
    try{ 
     BufferedReader bufferedReader = new BufferedReader(
             new FileReader("templates\\sqlQueries\\GetPlayerDetails.txt") 
                  ); 
     while(bufferedReader.readLine()!=null){ 
      query = new StringBuilder().append(query).append(bufferedReader.readLine()).toString(); 
     } 
    } 
    catch (FileNotFoundException e){ 
     e.printStackTrace(); 
    } 
    catch (IOException e){ 
     e.printStackTrace(); 
    } 
    query = query.replace("$sessionID", sessionID); 
+0

我建議孩子的步驟。嘗試選擇許多字段的嘗試僅選擇一個字段。這使得麻煩更輕鬆。 –

回答

7

您只需要在Java字符串文字中轉義雙引號。如果您正在從文件中讀取SQL查詢,那麼Java對在文件中未轉義的雙引號沒有任何問題。

取出文件中雙引號的所有轉義符,它應該可以正常工作。

p.first_name   "FirstName", 

此外,創建StringBuilderwhile循環之前,所以你你不要每次都從頭開始,而你不讀每次迭代的兩行:

StringBuilder sb = new StringBuilder(); 
String line; 
while ((line = bufferedReader.readLine()) != null) 
{ 
    sb.append(line); 
} 
query = sb.toString(); 

此外,代替在會話ID值(這將起作用)的末尾替換單引號,請使用JDBC佔位符,並在執行查詢之前使用PreparedStatement綁定會話ID。這將防止可能的SQL注入嘗試,例如如果sessionID是字符串:

Robert'); DROP TABLE players; -- 
+0

非常感謝! – mrk2