test;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;public classHelloWorld {public static voidmain(String []args) {
Connection connection=null;
Statement statement=null;try{
String url="jdbc:postgresql://127.0.0.1:5432/postgis";
String user="postgres";
String password= "123456";
Class.forName("org.postgresql.Driver");
connection=DriverManager.getConnection(url, user, password);
System.out.println("是否成功连接pg数据库"+connection);
String sql="select name from test";
statement=connection.createStatement();
ResultSet resultSet=statement.executeQuery(sql);while(resultSet.next()){
String name=resultSet.getString(1);
System.out.println(name);
}
}catch(Exception e){throw newRuntimeException(e);
}finally{try{
statement.close();
}catch(SQLException e){
e.printStackTrace();throw newRuntimeException(e);
}finally{try{
connection.close();
}catch(SQLException e){
e.printStackTrace();throw newRuntimeException(e);
}
}
}
}
}
运行结果:
Java连接postgresql数据库
标签:targe jdbc print url stat lib package ring next
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉
本文系统来源:http://www.cnblogs.com/shijingjing07/p/6354487.html