Created
February 1, 2020 21:30
-
-
Save Dviejopomata/2aaceb22883a34221f674e244f6bee10 to your computer and use it in GitHub Desktop.
This file contains 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.*; | |
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