-
-
Save catalinazz/272f0ba46c61d4e7c8bf5afe0576e3be to your computer and use it in GitHub Desktop.
Conexion con base de datos 24.04.2018
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
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