加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL 的一个简单连接和查询操作——示例

发布时间:2020-12-13 17:50:51 所属栏目:百科 来源:网络整理
导读:表和数据: CREATE TABLE weather ( city varchar(80),temp_lo int,-- low temperature temp_hi int,-- high temperature prcp real,-- average temperature date date);insert into weather values ('Wuhan',15,30,25.5,'2010-09-21');insert into weather

表和数据:

CREATE TABLE weather (
    city            varchar(80),temp_lo         int,-- low temperature
    temp_hi         int,-- high temperature
    prcp            real,-- average temperature
    date            date
);

insert into weather values ('Wuhan',15,30,25.5,'2010-09-21');
insert into weather values ('Beijing',10,22,15.3,'2010-09-22');
insert into weather values ('Shanghai',17,35,28.6,'2010-09-22');
insert into weather values ('Guangzhou',36,32.7,'2010-09-22');
insert into weather values ('Xiamen',24,32,30.3,'2010-09-22');
示例类:
public class TestConnPostgreSQL {

	public static void main(String[] args) {
		try {
			Class.forName("org.postgresql.Driver").newInstance();
			String url = "jdbc:postgresql://localhost:5432/testdb";
			Connection conn = DriverManager.getConnection(url,"postgres","postgres");
			
			Statement st = conn.createStatement();
			String sql = "select * from weather";
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				System.out.print( rs.getString("city") + "t");
				System.out.print( rs.getString("temp_lo") + "t");
				System.out.print( rs.getString("temp_hi") + "t");
				System.out.print( rs.getString("prcp") + "t");
				System.out.println( rs.getString("date"));
			}
			
			rs.close();
			st.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
输出结果:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读