2017-07-03 56 views
0

我試圖得到只從我的數據庫旅行,其中source_name,目的地是特定的。我想要使​​用準備好的語句從我的數據庫中獲取旅行清單。但我無法得到相同的結果。我在我的數據庫中手動檢查,但我的列表返回空?

package com.indiabus1.Dao; 

import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.List; 

import com.indiabus1.beans.*; 
import com.indiabus1.Dao.*; 
import com.indiabus1.*; 
import com.indiabus1.Dao.ConnectionFactory; 
import com.indiabus1.beans.BusSelectionBeans; 

public class BusSelectionDAO { 


Connection connection; 

public BusSelectionDAO(){} 

private static Connection getConnection() 
     throws SQLException, 
      ClassNotFoundException 
{ 
    Connection con = ConnectionFactory. 
      getInstance().getConnection(); 
    return con; 
} 




public static List<BusSelectionBeans> getRecords(String s, String s2){ 

    List<BusSelectionBeans> list=new ArrayList<BusSelectionBeans>(); 

    try{ 

     Connection con=getConnection(); 

     System.out.println(s+" to "+s2); 

     String SQL="select travels from new_trips.dt_trips where      
     source_name='"+s+"' and destination_name='"+s2+"' limit 5"; 
     PreparedStatement ps=con.prepareStatement(SQL); 

     //ps.setString(1, s); 
     // ps.setString(2, s2); 

     ResultSet rs=ps.executeQuery(); 

     while(rs.next()){ 

     BusSelectionBeans d=new BusSelectionBeans(); 

     String x1=rs.getString(1); 


     // String x2=rs.getString(1); 
     // String x3=rs.getString(2); 


     d.setBusOperator(x1); 

     // d.setS_name(x2); 
     // d.setD_name(x3); 



     list.add(d); 

     } 

     con.close(); 

    }catch(Exception e){System.out.println(e);} 


     System.out.println(list); 

    return list; 
    } 


    } 

爲什麼我得到我的列表返回空,但手動SQL查詢結果給出。

+0

現在在您的代碼中,您不會爲語句設置任何字符串。打印出當前正在執行的查詢並檢查是否給出結果。還可以使用[預準備語句](https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html)?作爲佔位符 – XtremeBaumer

+0

我已檢查,但它仍然給它的空列表... –

+0

首先,我不知道爲什麼你使用preparedStatement,當你沒有一個preparedStatement作爲查詢,你應該使用的executeQuery。 –

回答

0

我不確定,但如果您通過此驗證結果,可能System.out.println(list);不會給你任何有意義的東西。

您可以遍歷列表並打印它們。 for(BusSelectionBeans d_ins : list){/*print stuff of d_ins*/}

同樣爲了SQL注入,在註釋掉的時候返回(返回)準備好的語句。

古怪的可能性
如果你沒有得到任何打印操作stdoutSystem.out),那麼它可能會被轉發到另一出流,或在某些情況下,它需要衝洗。請確保它。
例如,將結果寫入文件中,而不是stdout(因爲它實際上不可能是stdout

+0

請選擇旅遊類型dt_trips其中source_name =?和destination_name =?limit 5「); ps.setString(1,s); ps.setString(2,s2); ResultSet rs = ps。的executeQuery(); ()rs_next()){ \t BusSelectionBeans d = new BusSelectionBeans(); String x1 = rs.getString(1); System.out.println(x1); // String x2 = rs.getString(1); // String x3 = rs.getString(2); d.setBusOperator(x1); –

+0

stll它沒有提供 –

+0

如果你沒有收到任何'exception/error',那麼查詢就不會返回任何結果。還要確保你正在處理正確的數據庫上下文。 – 2017-07-03 06:30:54

0

我以前也經歷過這種情況。 以下是可能幫助的事情:

  1. ,你可能想要做的第一件事是仔細檢查你的數據庫連接。你確定你連接到正確的數據庫嗎?有些情況下,您使用2個數據庫,而您忘記更改您使用的數據庫。例如official_db,official_db_test或db1和db2。
  2. 檢查你的參數s和s2?它們的值可能存在問題,因爲它們是字符串,並且在解析過程中可能有問題。
  3. 要確定rs內是否有值,請嘗試在(rs.next()){....}內放入System.out.println(「rs has row/s。」)。
+0

我已檢查並確認,...我的連接是完美的,s, s2是好的,他們沒事....但仍然只給空... –

+0

我明白了......我沒有修剪..s2。 –

相關問題