src根目录下写一个自定义属性文件:test.properties dbName = org.postgresql.Driver URL = jdbc:postgresql://192.168.13.47:5432/pdosmod name = postgres pass = postgres 利用Java自带的util读文件 package com.nfschina.test; import java.io.IOException; import java.util.Properties; public class ReadPorperties { public static String CLASSNAME; public static String DBURL; public static String USERNAME; public static String PASSWORD; public static Properties properties; static { properties = new Properties(); try { properties.load(Object.class .getResourceAsStream("/connection.properties")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } CLASSNAME = (String) properties.get("dbName"); DBURL = (String) properties.get("URL"); USERNAME = (String) properties.get("name"); PASSWORD = (String) properties.get("pass"); } } 用个java引用该文件就OK了 Just Easy. package com.nfschina.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { public static Connection getConnection() throws SQLException { System.out.println(ReadPorperties.CLASSNAME); try { Class.forName(ReadPorperties.CLASSNAME); } catch (ClassNotFoundException e) { e.printStackTrace(); } return DriverManager.getConnection( ReadPorperties.DBURL, ReadPorperties.USERNAME, ReadPorperties.PASSWORD); } public static void closeConnection(Connection conn) throws SQLException { conn.close(); } public static void main(String[] args) { try { String sql = "SELECT * FROM message"; Connection conn = getConnection(); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString("m_message")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }