Advertisement
Guest User

Untitled

a guest
Jun 11th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. package mcgill;
  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.logging.Level;
  9. import java.util.logging.Logger;
  10. import javax.swing.JOptionPane;
  11.  
  12. public class Main {
  13.  
  14.     public static void main(String[] args) {
  15.  
  16.         Connection conn = null;
  17.         PreparedStatement ps = null;
  18.  
  19.         try {
  20.  
  21.             Class.forName("com.mysql.jdbc.Driver");
  22.             String url = "jdbc:mysql://localhost/contatosdb";
  23.             conn = DriverManager.getConnection(url, "root", "root");
  24.             JOptionPane.showMessageDialog(null, "Conexão estabelecida");
  25.  
  26. //            for (int i = 1; i < 3; i++) {
  27. //                String sql = "insert into pessoas(nome, email) values(?, ?)";
  28. //                String nome = JOptionPane.showInputDialog("Digite seu nome.");
  29. //                String email = JOptionPane.showInputDialog("Digite seu email.");
  30. //
  31. //                ps = conn.prepareStatement(sql);
  32. //                ps.setString(1, nome);
  33. //                ps.setString(2, email);
  34. //                ps.executeUpdate();
  35. //
  36. //                JOptionPane.showMessageDialog(null, "Operação realizada.");
  37. //            }
  38.  
  39.  
  40.             String sql = "select * from pessoas";
  41.             ps = conn.prepareStatement(sql);
  42.             ResultSet rs = ps.executeQuery();
  43.             String resultado = "";
  44.  
  45.             while (rs.next()) {
  46.                 resultado += "\n\nNome: " + rs.getString("nome");
  47.                 resultado += "\nEmail: " + rs.getString("email");
  48.             }
  49.  
  50.             JOptionPane.showMessageDialog(null, resultado);
  51.  
  52.  
  53.  
  54.         } catch (SQLException ex) {
  55.             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  56.         } catch (ClassNotFoundException ex) {
  57.             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  58.         }
  59.  
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement