2010-07-01 96 views
-3

這是一個函數,它在接受字符串s(這是患者表中的諮詢字段)後從醫生表中返回最小醫生ID。例如,如果我在現場寫了「心臟病學」,那麼它會返回與該字段有關的最低醫生ID。幫助解決JDBC問題

此外,如果醫生是免費的,將由其現狀決定。

默認情況下,醫生是免費的,並且在他被分配後將被改爲是。

SQL語句存在問題。

public int getDocID(String s) 
{ 
    int did = 0; 
    try { 
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
     Connection con = 
      DriverManager.getConnection("jdbc:odbc:patientDSN"); 
     Statement stat = con.createStatement(); 
     ResultSet rs = 
      stat. 
      executeQuery 
      ("select min(Doc_ID) from Doctor where (Doc_CurrentStatus='No' and Doc_Speciality like '%" 
      + s + "'%'"); 
     if (rs.next()) { 
      did = rs.getInt(1); 
     } 
     System.out.println(did); 
     PreparedStatement ps1 = 
      con. 
      prepareStatement 
      ("UPDATE Doctor SET Doc_CurrentStatus='Yes' where Doc_ID = " 
      + did + ""); 
     ps1.executeUpdate(); 
    } 

    catch(Exception e) { 
     e.printStackTrace(); 
    } 
    return did; 
} 
+0

您必須格式化好。你使用的是什麼DBMS?什麼連接器?粘貼StackTrace – santiagobasulto 2010-07-01 15:44:15

回答

3

你有一個

'

報價比你需要更多。這是之前的最後

「%」

百分比符號。您也必須關閉最後的括號。你可能想看看PrepareStatement。鏈接Java Tutorial

ResultSet rs = stat.executeQuery(
    "select min(Doc_ID) from Doctor where (
     Doc_CurrentStatus='No' and Doc_Speciality like '%"+s+"%' 
    )" 
); 

可以執行此查詢:

select Doc_ID from Doctor where (Doc_CurrentStatus='No' and Doc_Speciality like '%"+s+"%') ORDER BY Doc_ID ASC LIMIT 1"); 

能提高peformance。

+0

謝謝!有效!! – 2010-07-01 16:13:32

+0

很高興聽到。你用ORDER BY子句改變了查詢嗎? – santiagobasulto 2010-07-01 16:22:30

+2

@aashima,歡迎來到SO!如果你對這個答案感到滿意,就像解決你的問題 - 聽起來像你一樣 - 你應該點擊投票櫃檯下的複選標記。這將標誌着它在系統中被接受,給聖地亞哥15代表,並給你兩個代表和一個徽章。每個人都贏了! – Pops 2010-07-01 16:41:40