Guest User

Untitled

a guest
May 2nd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. package dario.java.db;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11. public class MainDb {
  12.  
  13. public static void main(String[] args) {
  14. try (Connection c = conectarDB("jdbc:mysql://localhost:3306/curso_mysql", "root", "")) {
  15.  
  16. Statement stInsert = c.createStatement();
  17. stInsert.execute("insert into articulos (nombre) values ('clavos')");
  18.  
  19. stInsert.execute("delete from articulos where codigo = 3");
  20.  
  21. stInsert.execute("update articulos set nombre = 'MODIFICADO' where codigo = 2");
  22.  
  23. Statement stSelect = c.createStatement();
  24. ResultSet rs = stSelect.executeQuery("Select * from articulos");
  25.  
  26. while (rs.next()) {
  27. int codigo = rs.getInt("codigo");
  28. String nombre = rs.getString("nombte");
  29. System.out.println(codigo + ":" + nombre);
  30. }
  31.  
  32. } catch (SQLException ex) {
  33. Logger.getLogger(MainDb.class.getName()).log(Level.SEVERE, null, ex);
  34. }
  35.  
  36. }
  37.  
  38. public static Connection conectarDB(String strConnection, String usuario, String clave) throws SQLException {
  39. return DriverManager.getConnection(strConnection, usuario, clave);
  40. }
  41.  
  42. }
Add Comment
Please, Sign In to add comment