2014-05-21 36 views
0

我試圖設置我的數據庫屬性,所以在將來用戶可以更改它,如果他們想要更改數據庫的路由。問題是當我嘗試設置全角冒號時,它總是添加一個反斜槓轉義字符。在Java中輸入全角冒號(:)到setProperty

我試過正常和雙重轉義,但它不起作用。

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.util.Properties; 

public class SetProps { 

public static void SetDefaultProps(){ 
    Properties prop = new Properties(); 
OutputStream output = null; 

try { 

    output = new FileOutputStream("./build/classes/configuracion.properties"); 

    // Set the database property 
    prop.setProperty("url", "jdbc:mysql://192.168.1.192:3306/ordenestaller"); 


// Save Properties 
    prop.store(output, null); 

} catch (IOException io) { 
    io.printStackTrace(); 
} finally { 
    if (output != null) { 
     try { 
      output.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 
} 
public static void main(String[] args) { 
     SetDefaultProps(); 
    } 
} 
+0

你不需要逃脫在MySQL網址什麼,冒號是一個普通的字符 –

+0

功能的setProperty隨時添加到:反斜槓/: –

+0

IST是不是「setProperty' - 方法。這是'商店'的方法,將添加// – Jens

回答

0

對java代碼中的db url進行硬編碼並不是一種好的編程習慣。 我建議你把這些URL放在一個屬性文件中,並在需要時使用ResouceBundle來讀取它們。

你可以有「information.properties」在類路徑中的某個地方文件和文件的內容可以

dbURL:jdbc:mysql://192.168.1.192:3306/ordenestaller 

,現在在你的代碼,你可以使用一個資源包

ResourceBundle bundle = ResourceBundle.getBundle("information"); 
String dbURL= bundle.getString("dbURL"); 
得到這個

這將有一個額外的好處,即如果DB URl發生更改,則不需要重新編譯您的java類。

希望它可以幫助

+0

非常感謝您,但我需要輸入全寬度冒號,因爲我正在設置目錄路徑,並且C:/ Documents and Settings ....正在重新添加間隙C \:/ –