您的位置:首页 > 编程语言 > Java开发

PostgreSQL jdbc ,通过java进行简单连接

2017-12-08 20:33 295 查看
1、下载postgreSQL-jdbc
https://jdbc.postgresql.org/download.html
2、获取连接

Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection(url,user,password);
3、插入数据、创建表、更新数据、删除数据

connection.setAutoCommit(false);//关闭自动提交
Statement stmt = connection.createStatement();
String sql = "SQL相关语句";
stmt.executeUpdate(sql);
4、查看数据库

connection.setAutoCommit(false);//关闭自动提交
Statement stmt = connection.createStatement();
String sql = "查询语句";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt(columnLabel);
String name = rs.getString(columnLabel);
float  salary = rs.getFloat(columnLabel);
}
5 关闭连接

rs.close();
stmt.close();
connection.close();
6 整个过程需要使用try catch语句
try{
jdbc语句
}catch (Exception e){
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: