2017-04-21 85 views
0

我想从RandevufiltreSorgu.java得到doktor [0],doktor [1],secilenBolum。我在randevusec中创建了RandevufiltreSorgu类的对象。但是当我运行查询时,我无法获得我想要的值。没有使用继承。我能做什么?从另一个Java类获取值

public class randevusec { 
    Connection conn = null; 
    PreparedStatement ps = null; 
    ResultSet rs = null; 

    public ResultSet saatler() throws SQLException, Exception { 
     randevufiltreSorgu nesne = new randevufiltreSorgu(); 
     try { 
      Class.forName("org.postgresql.Driver"); 
      conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/tipmerkezivt", "postgres", "2569"); 
      ps = conn.prepareStatement(
        "WITH Q1 AS(\n" + " select * from doktor natural join bolum where dr_adi='" + nesne.doktor[0] 
          + "' and dr_soyadi='" + nesne.doktor[1] + "' and bolum.b_adi='" + nesne.secilenBolum + "'\n" 
          + " )\n" + " select cs_saat from doktorun_programi natural join Q1\n" + " ", 
        ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); 
      rs = ps.executeQuery(); 
      return rs; 
     } catch (Exception e) { 
      throw new Exception("Bağlantı başarısız!"); 
     } 

     finally { 
      conn.close(); 
     } 
    } 
} 
+0

doktor的类型是什么?它是一个字符串吗? –

+2

你应该查看一个JDBC教程,该教程描述了如何使用准备好的语句等来正确地执行此操作。一般来说,将JDBC资源(如结果集或语句)声明为类中的字段或在方法之间传递它们是不好的做法,因为它们应该具有最小范围(您想尽快关闭/释放它们)。取决于你的JDBC驱动程序的实现,因为你总是调用'conn.close();'方法结束时,结果集也可能会被关闭。而不是返回结果集,遍历它并返回一个POJO集合。 –

回答

0

这是很难理解你的代码,因为它主要是外语,但是当你构建doktor[0]doktor[1],在他们面前把关键字public static你可以做的是。