Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mcgill;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- public class Main {
- public static void main(String[] args) {
- Connection conn = null;
- PreparedStatement ps = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost/contatosdb";
- conn = DriverManager.getConnection(url, "root", "root");
- JOptionPane.showMessageDialog(null, "Conexão estabelecida");
- // for (int i = 1; i < 3; i++) {
- // String sql = "insert into pessoas(nome, email) values(?, ?)";
- // String nome = JOptionPane.showInputDialog("Digite seu nome.");
- // String email = JOptionPane.showInputDialog("Digite seu email.");
- //
- // ps = conn.prepareStatement(sql);
- // ps.setString(1, nome);
- // ps.setString(2, email);
- // ps.executeUpdate();
- //
- // JOptionPane.showMessageDialog(null, "Operação realizada.");
- // }
- String sql = "select * from pessoas";
- ps = conn.prepareStatement(sql);
- ResultSet rs = ps.executeQuery();
- String resultado = "";
- while (rs.next()) {
- resultado += "\n\nNome: " + rs.getString("nome");
- resultado += "\nEmail: " + rs.getString("email");
- }
- JOptionPane.showMessageDialog(null, resultado);
- } catch (SQLException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement