本文介绍了java连接postgresql数据库的示例代码,分享给大家,具体如下:
1.下载驱动jar
下载地址:
2.导入jar包
新建lib文件夹,将下载的jar驱动包拖到文件夹中。
将jar驱动包添加到libraries
3.程序代码如下:helloworld.java
package test; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; public class helloworld { public static void main(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 new runtimeexception(e); }finally{ try{ statement.close(); } catch(sqlexception e){ e.printstacktrace(); throw new runtimeexception(e); }finally{ try{ connection.close(); } catch(sqlexception e){ e.printstacktrace(); throw new runtimeexception(e); } } } } }
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
unity的错误解决办法:NullReferenceException: Object reference not set to an instance of an object;tiny proje
Hadoop 之 HDFS (HDFS 数据流的 读写 流程)
听说你一读Spring源码就懵逼?我帮你把架子搭好了,你填就行!
首席架构师推荐:金融保险领域数字化转型实践--如何优雅地修改业务中台中分层应用Maven多模块的版本号?(命令导入式)
[JVM学习之路]一、初识JVM,了解其结构、模型及生命周期
网友评论