Created
April 19, 2011 18:13
-
-
Save ProgDan/929108 to your computer and use it in GitHub Desktop.
Exemplo de conexão JDBC com um servidor Oracle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
👍
muito bom
Esse exemplo foi muito bom.
O driver do JDBC precisa ser colocado em qual pasta?
danielnschmitz Você conseguiu descobrir a resposta?
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
Agradecimentos!
Me foi muito útil seu exemplo 👍