packagecom.uncle;importjava.sql.Connection;importjava.sql.DriverManager;/**
* @program: postgresql-project
* @description: 连接postgresql
* @author: Sun Jinxin
* @create: 2021/11/24 10:35
*/publicclassPostgresqlJDBC{
publicstaticvoidmain(String args[]){
Connection c =null;try{
Class.forName("org.postgresql.Driver");
c =DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres","postgres","123456");}catch(Exception e){
e.printStackTrace();System.err.println(e.getClass().getName()+": "+e.getMessage());System.exit(0);}System.out.println("Opened database successfully");}}
packagecom.uncle;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.Statement;/**
* @program: postgresql-project
* @description: 连接postgresql
* @author: Sun Jinxin
* @create: 2021/11/24 10:35
*/publicclassPostgresqlJDBC{
publicstaticvoidmain(String args[]){
Connection c =null;Statement stmt =null;try{
Class.forName("org.postgresql.Driver");
c =DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres","postgres","123456");
stmt = c.createStatement();String sql ="CREATE TABLE COMPANY02 "+"(ID INT PRIMARY KEY NOT NULL,"+" NAME TEXT NOT NULL, "+" AGE INT NOT NULL)";
stmt.executeUpdate(sql);
stmt.close();
c.close();}catch(Exception e){
e.printStackTrace();System.err.println(e.getClass().getName()+": "+e.getMessage());System.exit(0);}System.out.println("成功了,欧耶!!!");}}
packagecom.uncle;importorg.postgresql.util.PSQLException;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.Statement;importjava.util.Random;importjava.util.UUID;/**
* @program: postgresql-project
* @description: 连接postgresql
* @author: Sun Jinxin
* @create: 2021/11/24 10:35
*/publicclassPostgresqlJDBC{
publicstaticvoidmain(String args[]){
Connection c =null;try{
Class.forName("org.postgresql.Driver");
c =DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres","postgres","123456");
c.setAutoCommit(false);System.out.println("连接数据库成功!");String sql ="INSERT INTO postgresqltable (ID,NAME,AGE) "+"VALUES (?, 'Sjx', 18);";for(int i =0; i <1000000; i++){
PreparedStatement preparedStatement = c.prepareStatement(sql);
preparedStatement.setString(1, UUID.randomUUID().toString());
preparedStatement.executeUpdate();System.out.println("还有 -》 "+(1000000-i)+" 次");}
c.commit();
c.close();}catch(Exception e){
e.printStackTrace();System.err.println(e.getClass().getName()+": "+e.getMessage());System.exit(0);}System.out.println("数据增加成功");}}