Skip to content

Instantly share code, notes, and snippets.

@ProgDan
Created April 19, 2011 18:13
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save ProgDan/929108 to your computer and use it in GitHub Desktop.
Save ProgDan/929108 to your computer and use it in GitHub Desktop.
Exemplo de conexão JDBC com um servidor Oracle
import java.sql.*;
public class TestOracle {
public static void main(String[] args) {
String sql;
/*
* O driver JDBC do Oracle está disponível no seguinte endereço:
* http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
*/
// Configuração dos parâmetros de conexão
String server = "localhost";
String port = "1521"; // Porta TCP padrão do Oracle
String database = "test";
// Configuração dos parâmetros de autenticação
String user = "root";
String passwd = "";
try {
String url = "jdbc:oracle:thin:@" + server + ":" + port + ":" + database;
// Abre-se a conexão com o Banco de Dados
Connection con = DriverManager.getConnection(url, user, passwd);
// Cria-se Statement com base na conexão con
Statement stmt = con.createStatement();
// Exemplo: cria-se uma tabela no Banco de Dados de Teste
sql = "CREATE TABLE `filmes` ("
+ "`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,"
+ "`titulo` VARCHAR(80) NOT NULL,"
+ "`ano` INT UNSIGNED,"
+ "`diretor` VARCHAR(80),"
+ "PRIMARY KEY (`id`))"
+ "CHARACTER SET utf8";
stmt.executeUpdate(sql);
// Exemplo: inserindo dados na tabela de filmes
sql = "INSERT INTO `filmes` (`titulo`, `ano`, `diretor`)"
+ "VALUES ('The Matrix', 1999, 'Andy Wachowski & Larry Wachowski')";
stmt.executeUpdate(sql);
sql = "INSERT INTO `filmes` (`titulo`, `ano`, `diretor`)"
+ "VALUES ('The Matrix Reloaded', 2003, 'Andy Wachowski & Larry Wachowski')";
stmt.executeUpdate(sql);
sql = "INSERT INTO `filmes` (`titulo`, `ano`, `diretor`)"
+ "VALUES ('The Matrix Revolutions', 2003, 'Andy Wachowski & Larry Wachowski')";
stmt.executeUpdate(sql);
// Exemplo: navegando e exibindo os dados dos filmes
sql = "SELECT `titulo`,`ano` FROM `filmes`";
// Executa-se a consulta dos campos titulo,ano da tabela de filmes
ResultSet res = stmt.executeQuery(sql);
int ano;
String titulo;
while (res.next()) {
ano = res.getInt("ano");
titulo = res.getString("titulo");
System.out.println("ROW = " + titulo + ": " + ano);
}
// Exemplo: excluindo a tabela filmes do Banco de Dados de Teste
sql = "DROP TABLE `filmes`";
stmt.executeUpdate(sql);
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@maiarar
Copy link

maiarar commented Dec 4, 2015

Agradecimentos!
Me foi muito útil seu exemplo 👍

Copy link

ghost commented Sep 11, 2016

👍

@alexkody
Copy link

muito bom

Copy link

ghost commented Apr 7, 2019

Esse exemplo foi muito bom.

@danielnschmitz
Copy link

O driver do JDBC precisa ser colocado em qual pasta?

@leandroSJ
Copy link

danielnschmitz Você conseguiu descobrir a resposta?

@Raphaelfm
Copy link

O driver do JDBC precisa ser colocado em qual pasta?

Na pasta lib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment