2010-07-01 171 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