Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jdbc1;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.util.Scanner;
- import java.sql.ResultSet;
- public class Consulta {
- public static void main(String[] args) {
- //Entrada de dados
- Scanner tecla = new Scanner(System.in);
- System.out.println("digite: ");
- String especialidade = tecla.next();
- try{
- // PASSO 1 CARREGAR O DRIVER
- Class.forName("com.mysql.jdbc.Driver");
- // PASSO 2 DEFINIR URL DE CONEXÃO
- Connection con =
- DriverManager.getConnection("jdbc:mysql://localhost:3306/clinica","root", "root");
- // PASSO 3 CRIAR
- String queryStr = "SELECT * FROM Medicos where especialidade = ? ;";
- //PAASSO 4
- PreparedStatement pstmt = con.prepareStatement(queryStr);
- pstmt.setString(1, especialidade);
- ResultSet rs = pstmt.executeQuery();
- while (rs.next()) {
- String nome = rs.getString("nome");
- int idade = rs.getInt("idade");
- String cidade = rs.getString("cidade");
- System.out.println("nome: " + nome + " - Idade: " + idade + "- Cidade: " + cidade);
- }
- rs.close();
- con.close();
- } catch (Exception e){
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement