Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package conexiondb;
- import java.sql.*;
- public class ConexionDB {
- public static void main(String[] args) {
- String nombre;
- String sql = "select * from Persona";
- Connection con = null;
- // Driver
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (Exception e) {
- System.err.println("Error en driver");
- }
- //conexion
- try {
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Agenda", "root", "n0m3l0");
- } catch (Exception e) {
- System.err.println("Error en conexion");
- }
- //sentencias
- try {
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- while (rs.next()) {
- nombre = rs.getString("nombrePersona");
- System.out.println("Nombre: \t" + nombre);
- }
- } catch (Exception e) {
- System.err.println("Error en sentencias");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement