转载自http://blog.csdn.net/wlwlwlwl015/article/details/48131073
Java测试类的代码:
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.Statement;
- public class Test {
- public static void main(String[] args) {
- try {
- Class.forName("org.postgresql.Driver").newInstance();
- String url = "jdbc:postgresql://localhost:5432/testdb";
- Connection con = DriverManager.getConnection(url, "postgres",
- "111111");
- Statement st = con.createStatement();
- String sql = " select * from t_user ";
- ResultSet rs = st.executeQuery(sql);
- ResultSetMetaData rsmd = rs.getMetaData();
- int columnCount = rsmd.getColumnCount();
- while (rs.next()) {
- for (int i = 1; i <= columnCount; i++) {
- System.out.print(rs.getString(i) + "\t");
- }
- System.out.println();
- }
- rs.close();
- st.close();
- con.close();
- } catch (Exception e) {
- System.out.print("exception:" + e.getMessage());
- }
- }
- }
以上代码经测试运行正常,最后再贴出postgresql 9.3的Maven依赖片段:
- <!-- postgresql -->
- <dependency>
- <groupId>org.postgresql</groupId>
- <artifactId>postgresql</artifactId>
- <version>9.3-1102-jdbc4</version>
- </dependency>