Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. import java.sql.*;
  2. import javax.sql.*;
  3.  
  4. public class main {
  5.  
  6.     public static void main(String[] args) {
  7.         Connection conn = null;
  8.            try
  9.            {
  10.  
  11.                String url = "jdbc:mysql://52.178.136.243:3306/Prueba";
  12.                Class.forName ("com.mysql.jdbc.Driver");
  13.                conn = DriverManager.getConnection (url,"root","1234asd");
  14.                System.out.println ("Database connection established");
  15.            }
  16.            catch (Exception e)
  17.            {
  18.                e.printStackTrace();
  19.  
  20.            }
  21.            
  22.            finally
  23.            {
  24.                if (conn != null)
  25.                {
  26.                    try
  27.                    {
  28.                        Statement stmt = null;
  29.                        stmt = conn.createStatement();
  30.                        String sql;
  31.                        sql = "SELECT * FROM Prueba.Cliente;";
  32.                        ResultSet rs = stmt.executeQuery(sql);
  33.                        while(rs.next()){
  34.                              //Retrieve by column name
  35.                              int id  = rs.getInt("idCliente");
  36.                              String name = rs.getString("name");
  37.  
  38.                              //Display values
  39.                              System.out.printf("ID: %d" , id);
  40.                              System.out.printf(", name: %s \n" , name);
  41.                          
  42.                           }
  43.                    }
  44.                    catch (Exception e) { /* ignore close errors */ }
  45.                }
  46.            }
  47.            
  48.            
  49.  
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement