aula5TJDBC

import java.sql.*;

class aula5TJDBC {

    static final String JDBC_DRIVEr = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/pd";

    static final String USER = "root";
    static final String PASS = "";
    static final String QUERY = "SELECT * FROM testepd";

    public static void main(String[] args) {
        // Open a connection
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            System.out.println("Ligar à base de dados..");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            System.out.println("Concretizar uma operação..");
            stmt = conn.createStatement();
            String sql = "SELECT * FROM testepd;";
            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                int id = rs.getInt("id");
                String nome = rs.getString("nome");
                String localidade = rs.getString("localidade");
                int idade = rs.getInt("idade");

                System.out.print("ID: " + id);
                System.out.print(", nome: " + nome);
                System.out.print(", localidade: " + localidade);
                System.out.println(", idade: " + idade);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try{
                if(stmt != null){
                    stmt.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try{
                if(conn != null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try{
                if(rs != null){
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Terminou");
    }
}

//versão google
/*
import java.sql.*;

public class FirstExample {
    static final String DB_URL = "jdbc:mysql://localhost/pd";
    static final String USER = "root";
    static final String PASS = "";
    static final String QUERY = "SELECT * FROM testepd";

    public static void main(String[] args) {
        // Open a connection
        try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(QUERY);) {
            // Extract data from result set
            while (rs.next()) {
                // Retrieve by column name
                System.out.print("ID: " + rs.getInt("id"));
                System.out.print(", nome: " + rs.getString("nome"));
                System.out.print(", localidade: " + rs.getString("localidade"));
                System.out.println(", idade: " + rs.getInt("idade"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
*/
Tags : ,

0 thoughts on “aula5TJDBC”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.