jdbc连接postgresql

废话不多说,RT~


首先简单看一下数据库中的数据:



下面是Java测试类的代码:

package com.wl.test;

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>


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页