Skip to content

Instantly share code, notes, and snippets.

@catalinazz
Created April 24, 2018 23:06
Show Gist options
  • Save catalinazz/272f0ba46c61d4e7c8bf5afe0576e3be to your computer and use it in GitHub Desktop.
Save catalinazz/272f0ba46c61d4e7c8bf5afe0576e3be to your computer and use it in GitHub Desktop.
Conexion con base de datos 24.04.2018
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment