Advertisement
Guest User

ConectionDB

a guest
Sep 23rd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. package conexiondb;
  2.  
  3. import java.sql.*;
  4.  
  5. public class ConexionDB {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         String nombre;
  10.         String sql = "select * from Persona";
  11.         Connection con = null;
  12.  
  13.         // Driver
  14.         try {
  15.             Class.forName("com.mysql.jdbc.Driver");
  16.         } catch (Exception e) {
  17.             System.err.println("Error en driver");
  18.         }
  19.         //conexion
  20.         try {
  21.             con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Agenda", "root", "n0m3l0");
  22.         } catch (Exception e) {
  23.             System.err.println("Error en conexion");
  24.         }
  25.         //sentencias
  26.  
  27.         try {
  28.             Statement stmt = con.createStatement();
  29.             ResultSet rs = stmt.executeQuery(sql);
  30.             while (rs.next()) {
  31.                 nombre = rs.getString("nombrePersona");
  32.                 System.out.println("Nombre: \t" + nombre);
  33.             }
  34.         } catch (Exception e) {
  35.             System.err.println("Error en sentencias");
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement