Skip to content

Instantly share code, notes, and snippets.

@Dviejopomata
Created February 1, 2020 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dviejopomata/2aaceb22883a34221f674e244f6bee10 to your computer and use it in GitHub Desktop.
Save Dviejopomata/2aaceb22883a34221f674e244f6bee10 to your computer and use it in GitHub Desktop.
import java.sql.*;
class SqlEjemplo {
public static void main(String[] args) throws Exception {
System.out.println("Hello world!");
// instalar la libreria
// https://mvnrepository.com/artifact/org.postgresql/postgresql/42.2.9
String user = "vxgdibal";
String password = "jGBKv3F7IVEaUzA6ajoWY8sXQWyLD9z9";
String url = "jdbc:postgresql://manny.db.elephantsql.com:5432/vxgdibal";
try (Connection conn = DriverManager.getConnection(url, user, password)) {
PreparedStatement preparedStatement = conn.prepareStatement("SELECT id, nombre from cliente");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("ID");
String nombre = resultSet.getString("nombre");
System.out.println(String.format("Cliente id=%s nombre=%s", id, nombre));
}
} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment