Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class UpdateTable {
- public static void main(String[] args) throws Exception {
- Connection c = null;
- Statement stat = null;
- try {
- // Cargar controlador de datos, base de datos
- Class.forName("org.postgresql.Driver");
- // Conéctese a la dirección de la base de datos, nombre, contraseña
- c= DriverManager
- .getConnection("jdbc:postgresql://127.0.0.1:5432/postgres", "postgres", "maxhito");
- c.setAutoCommit(false);
- System.out.println("Opened database successfully");
- stat = c.createStatement();
- String sql = "UPDATE BBQ SET AGE=15 WHERE ID=2 ;";
- stat.executeUpdate(sql);
- c.commit();
- // Consulta la base de datos
- ResultSet rs = stat.executeQuery("SELECT * FROM BBQ;");
- while ( rs.next() ) {
- int id = rs.getInt("id");
- String name = rs.getString("name");
- int age = rs.getInt("age");
- String address = rs.getString("address");
- System.out.println( "ID = " + id );
- System.out.println( "NAME = " + name );
- System.out.println( "AGE = " + age );
- System.out.println( "ADDRESS = " + address );
- System.out.println();
- }
- rs.close();
- stat.close();
- c.close();
- } catch (ClassNotFoundException e) {
- System.err.println( e.getClass().getName()+": "+ e.getMessage() );
- System.exit(0);
- }
- System.out.println("Operation done successfully");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement