Está en la página 1de 2

****IMPORTACIONES****

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
****DECLARACION DE VARIABLES****
private Connection connection = null;
private ResultSet rs = null;
private Statement s = null;
****INICIAR CONEXIN****

if (connection != null) {
return;
}
String url = "jdbc:postgresql://localhost:5432/postgres";
String password = "1234";
try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(url, "postgres", password);
if (connection != null) {
System.out.println("Conectando a Base de Datos...");
}
} catch (Exception e) {
System.out.println("Problemas de Conexin");
}

****INSERCCIN DE REGISTROS****
try {
s = connection.createStatement();
int z = s.executeUpdate("INSERT INTO suscriptores (nombre) VALUES ('
" + name + "')");
if (z == 1) {
System.out.println("Se agreg el registro de manera exitosa");
} else {
System.out.println("Ocurrio un problema al agregar el registro")
;
}
} catch (Exception e) {
System.out.println("Error de conexion");
}
****REALIZANDO CONSULTAS****
try {
s = connection.createStatement();
rs = s.executeQuery("SELECT name FROM suscriptores WHERE nombre='" +
name + "'");
} catch (Exception e) {
System.out.println("Problema Buscando La Base de Datos");
}
****IMPRIMIR RESULTADOS DE CONSULTAS****
String string = "";

try {
while (rs.next()) {
string += rs.getString(1) + "\n";
System.out.print(string);
string = "";
}
} catch (Exception e) {
System.out.println("Problema al imprimir la informacin.");
}
**** CREAR TABLA EN POSTGRES****
CREATE TABLE suscriptores (
nombre CHAR(15)
);

También podría gustarte