package com.demo.tools;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class PostgreSQLUtils {
private static final String DATABASE_NAME = "xsmt";
private static final String USER_NAME = "postgres";
private static final String PASS_WORD = "root";
private static Connection c = null;
private static Statement stmt = null;
public static Connection getConnection() {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
try {
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/"+DATABASE_NAME,USER_NAME,PASS_WORD);
} catch (SQLException e) {
e.printStackTrace();
}
return c;
}
public static void closeConnection() {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
c.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static boolean createTable(String sql) {
PostgreSQLUtils.createTable(sql);
System.out.println("创建成功");
boolean flag = false;
try {
stmt = c.createStatement();
flag = true;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
closeConnection();
return flag;
}
public static boolean save(String sql) {
boolean flag = false;
try {
stmt = c.createStatement();
stmt.executeUpdate(sql);
flag = true;
} catch (SQLException e1) {
e1.printStackTrace();
flag = false;
}
closeConnection();
return flag;
}
public static boolean delete(String sql) {
boolean flag = false;
try {
stmt = c.createStatement();
stmt.executeUpdate(sql);
flag = true;
} catch (SQLException e1) {
e1.printStackTrace();
flag = false;
}
closeConnection();
return flag;
}
public static boolean update(String sql) {
boolean flag = false;
try {
stmt = c.createStatement();
stmt.executeUpdate(sql);
flag = true;
} catch (SQLException e1) {
e1.printStackTrace();
flag = false;
}
closeConnection();
return flag;
}
}