Advertisement
robeeeert

Untitled

Nov 10th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. package conexion;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.Scanner;
  9.  
  10. public class Conect {
  11.     public static void main(String[] args) {
  12.         Connection connection = null;
  13.         String url = "jdbc:mariadb://localhost:3306/bdclientes";
  14.         String user = "root";
  15.         String pwd = "6321";
  16.  
  17.         try {
  18.             connection = DriverManager.getConnection(url, user, pwd);
  19.  
  20.         } catch (SQLException e) {
  21.             e.printStackTrace();
  22.         } finally {
  23.             if (connection != null) {
  24.              try {
  25.                 connection.close();
  26.                 } catch (SQLException e) {
  27.                     e.printStackTrace();
  28.                 }
  29.             }
  30.         }
  31.     }
  32.     public static void insertarcliente(Connection connection, String nombre, int nit) throws SQLException {
  33.         String sql = "INSERT INTO clientes (nombre, nit) VALUES (?, ?)";
  34.         try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
  35.             preparedStatement.setString(1, nombre);
  36.             preparedStatement.setInt(2, nit);
  37.             preparedStatement.executeUpdate();
  38.             System.out.println("Cliente agregago");
  39.         }
  40.     }
  41.  
  42.     public static void LeerClientes(Connection connection) throws SQLException {
  43.         String sql = "SELECT * FROM clientes";
  44.         try (PreparedStatement preparedStatement = connection.prepareStatement(sql);
  45.              ResultSet resultSet = preparedStatement.executeQuery()) {
  46.             while (resultSet.next()) {
  47.              String nombre = resultSet.getString("nombre");
  48.              int nit = resultSet.getInt("nit");
  49.              System.out.println("Nombre: " + nombre + ", Nit: " + nit);
  50.             }
  51.         }
  52.     }
  53.  
  54.     public static void actualizarcliente(Connection connection, String nombre, int nit) throws SQLException {
  55.         String sql = "UPDATE clientes  SET nit = ? WHERE nombre = ?";
  56.         try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
  57.             preparedStatement.setString(2, nombre);
  58.             preparedStatement.setInt(1, nit);
  59.             preparedStatement.executeUpdate();
  60.             System.out.println("Cliente ACTUALIZADO.");
  61.         }
  62.     }
  63.  
  64.     public static void eliminarcliente(Connection connection, String nombre) throws SQLException {
  65.         String sql = "DELETE FROM clientes WHERE nombre = ?";
  66.         try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
  67.             preparedStatement.setString(1, nombre);
  68.             preparedStatement.executeUpdate();
  69.             System.out.println("cliente eliminado");
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement