Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dario.java.db;
- 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;
- public class MainDb {
- public static void main(String[] args) {
- try (Connection c = conectarDB("jdbc:mysql://localhost:3306/curso_mysql", "root", "")) {
- Statement stInsert = c.createStatement();
- stInsert.execute("insert into articulos (nombre) values ('clavos')");
- stInsert.execute("delete from articulos where codigo = 3");
- stInsert.execute("update articulos set nombre = 'MODIFICADO' where codigo = 2");
- Statement stSelect = c.createStatement();
- ResultSet rs = stSelect.executeQuery("Select * from articulos");
- while (rs.next()) {
- int codigo = rs.getInt("codigo");
- String nombre = rs.getString("nombte");
- System.out.println(codigo + ":" + nombre);
- }
- } catch (SQLException ex) {
- Logger.getLogger(MainDb.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static Connection conectarDB(String strConnection, String usuario, String clave) throws SQLException {
- return DriverManager.getConnection(strConnection, usuario, clave);
- }
- }
Add Comment
Please, Sign In to add comment