Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7. public class prueba{
  8. public static void main(String args[]){
  9. try {
  10. //Cargar clase de controlador de base de datos
  11. Class.forName("com.mysql.jdbc.Driver");
  12. //Crear el objeto de conexion a la base de datos
  13. Connection conexion = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ejemplo","mikel","abc123");
  14. //Crear objeto Statement para realizar queries a la base de datos
  15. Statement instruccion = conexion.createStatement();
  16. //Un objeto ResultSet, almacena los datos de resultados de una consulta
  17. ResultSet tabla = instruccion.executeQuery("SELECT cod , nombre FROM datos");
  18. //System.out.println("Codigo\tNombre");
  19. while(tabla.next())
  20. System.out.println(tabla.getInt(1)+"\t"+tabla.getString(2));
  21. }
  22. catch(ClassNotFoundException e)
  23. {
  24. System.out.println(e);
  25. }
  26. catch(SQLException e)
  27. {
  28. System.out.println(e);
  29. }
  30. catch(Exception e)
  31. {
  32. System.out.println(e);
  33. }
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement