Created
August 17, 2018 00:06
-
-
Save gabrielsaints/d82875f65ecfcfa6cf06e9f19062114b to your computer and use it in GitHub Desktop.
PostgreSQL class for Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
import javax.swing.*; | |
import java.util.*; | |
public class PostgreSQL{ | |
private int _code; | |
private String _type; | |
private String[] database = {"postgres", "sqladmin", "org.postgresql.Driver", "jdbc:postgresql://localhost:5432/banco73b2017"}; | |
private Connection _pgsql; | |
private ResultSet _result; | |
private PreparedStatement _pStatement; | |
private Statement _statement; | |
public PostgreSQL(){ | |
} | |
public void setCode(int value){ | |
this._code = value; | |
} | |
public void setType(String value){ | |
this._type = value; | |
} | |
public int getCode(){ | |
return this._code; | |
} | |
public String getType(){ | |
return this._type; | |
} | |
public void insert(String table, ArrayList<String> list){ | |
String sql_in = "INSERT INTO "+table+" VALUES("; | |
this._pStatement = _pgsql.prepareStatement(null); | |
try{ | |
int cont = 0; | |
for(String any : list){ | |
if(cont == 0){ | |
sql_in+="?"; | |
} | |
else{ | |
sql_in+=", ?"; | |
} | |
_pStatement.setString(cont++, any); | |
} | |
sql_in+= ")"; | |
_pStatement.execute(sql_in); | |
_pStatement.close(); | |
} | |
catch(Exception e){ | |
} | |
} | |
public void select(String table, ArrayList<String> list){ | |
String sql_in = "SELECT "; | |
this._pStatement = _pgsql.prepareStatement(null); | |
try{ | |
int cont = 0; | |
for(String any : list){ | |
if(cont == 0){ | |
sql_in+="?"; | |
} | |
else{ | |
sql_in+=", ?"; | |
} | |
_pStatement.setString(cont++, any); | |
} | |
sql_in+=" FROM "+table; | |
_pStatement.execute(sql_in); | |
_pStatement.close(); | |
} | |
catch(Exception e){ | |
} | |
} | |
public void select(String table, ArrayList<String> list, ArrayList<String> where){ | |
String sql_in = "SELECT "; | |
this._pStatement = _pgsql.prepareStatement(null); | |
try{ | |
int cont = 0; | |
for(String any : list){ | |
if(cont == 0){ | |
sql_in+="?"; | |
} | |
else{ | |
sql_in+=", ?"; | |
} | |
_pStatement.setString(cont++, any); | |
} | |
sql_in+=" FROM "+table; | |
sql_in+= " WHERE TRUE "; | |
for(String any : where){ | |
sql_in+= (" AND "+any); | |
} | |
_pStatement.execute(sql_in); | |
_pStatement.close(); | |
} | |
catch(Exception e){ | |
} | |
} | |
public void drop(String table, ArrayList<String> where){ | |
String sql_in = "DELETE FROM "+table+" WHERE TRUE"; | |
this._pStatement = _pgsql.prepareStatement(null); | |
try{ | |
int cont = 0; | |
for(String any : where){ | |
sql_in+=(" AND"+any); | |
} | |
_pStatement.execute(sql_in); | |
_pStatement.close(); | |
} | |
catch(Exception e){ | |
} | |
} | |
public void update(String table, ArrayList<String> list, ArrayList<String> where){ | |
String sql_in = "UPDATE "+table+" SET"; | |
this._pStatement = _pgsql.prepareStatement(null); | |
try{ | |
int cont = 0; | |
for(String any : list){ | |
if(cont == 0){ | |
sql_in+=any; | |
} | |
else{ | |
sql_in+=(", "+any); | |
} | |
cont++; | |
} | |
sql_in+= " WHERE TRUE "; | |
for(String any : where){ | |
sql_in+=(" AND"+any); | |
} | |
_pStatement.execute(sql_in); | |
_pStatement.close(); | |
} | |
catch(Exception e){ | |
} | |
} | |
public void connect(){ | |
try{ | |
Class.forName(database[3]); | |
_pgsql=DriverManager.getConnection(database[2],database[0],database[1]); | |
} | |
catch(Exception e){ | |
} | |
} | |
public void disconnect(){ | |
try{ | |
_pgsql.close(); | |
} | |
catch(Exception e){ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It isn't finished yet