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

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);
 }


猜你喜欢

转载自blog.csdn.net/masorl/article/details/78755311