Skip to content

Instantly share code, notes, and snippets.

@MuriEdu
Created May 5, 2025 01:41
Show Gist options
  • Save MuriEdu/2b0cb46dea9ab74f141770c606b67044 to your computer and use it in GitHub Desktop.
Save MuriEdu/2b0cb46dea9ab74f141770c606b67044 to your computer and use it in GitHub Desktop.
PC.04 - Murilo Ramos
package br.ufscar.dc.dsw;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AcessaBD {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/Livraria";
Connection con = (Connection) DriverManager.getConnection(url,
"root", "root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from Livro");
while (rs.next()) {
System.out.print(rs.getString("Titulo"));
System.out.print(", " + rs.getString("Autor"));
System.out.print(", " + rs.getInt("Ano"));
System.out.println(" (R$ " + rs.getFloat("Preco") + ")");
}
stmt.close();
con.close();
} catch (ClassNotFoundException e) {
System.out.println("A classe do driver de conexão não foi encontrada!");
} catch (SQLException e) {
System.out.println("O comando SQL não pode ser executado!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment