Java y MySQL

package primeraconexion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class PrimeraConexion {

public static void connect(){

String url = «jdbc:mysql://localhost:3306/nombre_base_de_datos»;
String user = «root»;
String pass = «»;

System.out.println(«Conectando…»);

try (Connection connection = DriverManager.getConnection(url, user,pass)) {

System.out.println(«Conectado!!»);
System.out.println(«Insertando …»);
Statement s = connection.createStatement();

// 3 Consultas básicas –

// insert into nombretabla (campo1,campo2,…) value (valor1,valor2,…)
// select nombrecampo front from nombretabla where … order by … limit
// UPDATE table_name SET column1 = value1, column2 = value2, … WHERE condition;

String consulta1 = «insert into tabla (campo) value (\»valor2\»)»;
s.executeUpdate(consulta1);

String nuevaconsulta = «select * from tabla»;
ResultSet rs = s.executeQuery(nuevaconsulta);

while (rs.next()) {

System.out.println(«campo= «+rs.getObject(«campo»));

}

rs.close();

} catch(SQLException e) {

System.out.println(e.getMessage());

}
}

public static void main(String[] args) throws SQLException {

PrimeraConexion conexion = new PrimeraConexion();
conexion.connect();

}

}

Write a Reply or Comment

Your email address will not be published.