0% found this document useful (0 votes)
23 views2 pages

Ejemplobd

The document shows how to connect to a MySQL database and perform CRUD operations using JDBC in Java. It imports the necessary JDBC drivers, establishes a connection to a MySQL database, and executes sample SQL statements to insert, update, delete, and select data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Ejemplobd

The document shows how to connect to a MySQL database and perform CRUD operations using JDBC in Java. It imports the necessary JDBC drivers, establishes a connection to a MySQL database, and executes sample SQL statements to insert, update, delete, and select data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

ejemplobd

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

public class ejemplobd {

public static void main (String[] args) {

try {

Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.print("imposible abrir el driver");

return;

try {

Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/"
+"ejemplo184?useTimezone=true&serverTimezone=UTC", "root", "");
Statement stmt = con.createStatement();

//insertar datos
// stmt.executeUpdate("INSERT INTO estudiantes ( nomEst ,
codEst) VALUES ('ye' , 3) ");

//modificar datos
// stmt.executeUpdate("UPDATE estudiantes SET nomEst ='pablo'
WHERE codEst= 3");

//eliminar datos
//stmt.executeUpdate("DELETE FROM estudiantes WHERE codEst= 311
");

//modificar datos
// stmt.executeUpdate("UPDATE estudiantes SET codEst = 10111
WHERE nomEst = 'joty'");

ResultSet rs = stmt.executeQuery("SELECT codEst, nomEst FROM


estudiantes ");
while (rs.next()) {

System.out.println(rs.getString("codEst")+"
"+rs.getString("nomEst"));

}
} catch (SQLException se ){

System.out.print("SQL Exepction:"+ se.getMessage());


se.printStackTrace(System.out);

You might also like