2014-04-07 134 views
1

我想將獲取到ResultSet對象中的數據庫與存儲在屬性文件中的數據庫進行比較。如果數據庫與屬性文件的dbname匹配,它將打印xxx.I提供下面的代碼。如何將結果集對象與字符串進行比較?

public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException { 
Properties props=new Properties(); 
props.load(new FileInputStream("/home/core/Desktop/Java/Sample Netbeans Projects/Project/PropStoreDb/StoreDbNameProps.properties"));   
Class.forName(props.getProperty("db.driver")); 
Connection con= DriverManager.getConnection("jdbc:mysql://localhost/", "root", ""); 
Statement st = con.createStatement(); 
    ResultSet rs = st.executeQuery("show databases"); 
    if(rs.next()){ 
     for(int i=1;i<=rs.getRow();i++){ 
      if(rs.getString(i).equals(props.getProperty("db.dbname"))) 
      { 
       System.out.println("xxx"); 
      } 
     } 
    } 

這裏是屬性文件

db.dbname=mydb 
db.url=jdbc:mysql://localhost/mydb 
db.driver=com.mysql.jdbc.Driver 

回答

1

更改代碼

while(rs.next()){ 
if(rs.getString(1)!=null && rs.getString(1).equals(props.getProperty("db.dbname"))) 
      { 
       System.out.println("xxx"); 
      } 
} 

沒有必要一個for循環。查詢show databases;將返回一個列的結果集。因此,您可以使用rs.getString(1)獲得數據,因爲列ID從1開始

+0

代碼運行良好..謝謝你 – skd

+0

歡迎:) – Keerthivasan

相關問題