Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.73 KB | None | 0 0
  1. package dbapp;
  2. import java.SQL.*; // importa a class
  3. import java.util.Scanner;
  4.  
  5. public class DBApp {
  6.  
  7.    
  8.     public static void main(String[] args)  throws SQLException {
  9.        
  10.         Connection conexao = DriverManager.getConnection(
  11.  "jdbc:oracle:thin:@//camburi.pucrs.br:1521/facin11g",
  12.  "be201628", "be201628");
  13.  
  14.         System.OUT.println("Conectado.");
  15.        
  16.  
  17.     /*  
  18.       String sql = "SELECT placa, ano, km, marca, modelo"
  19.  + " FROM VEICULOS";
  20. PreparedStatement stmt = conexao.prepareStatement(sql);
  21. // Executa o comando SELECT
  22. ResultSet rs = stmt.executeQuery();
  23. // Itera sobre todos os registros do ResultSet
  24. while (rs.next()) {
  25.  String placa = rs.getString("placa");
  26.  Integer ano = rs.getInt("ano");
  27.  Integer km = rs.getInt("km");
  28.  String marca = rs.getString("marca");
  29.  String modelo = rs.getString("modelo");
  30.  System.out.println(placa
  31.  + " " + ano
  32.  + " " + km
  33.  + " " + marca
  34.  + " " + modelo
  35.  );
  36. }
  37. /*
  38. // Fecha todos os objetos
  39. rs.close();
  40. stmt.close();
  41. conexao.close();
  42.  
  43.  
  44.  
  45. */
  46.    
  47.     String SQL = "INSERT INTO VEICULOS"
  48.  + " (placa, ano, km, marca, modelo)"
  49.  + " VALUES (?, ?, ?, ?, ?)";
  50.  // 1 2 3 4 5 (numeração dos parâmetros)
  51. PreparedStatement stmt = conexao.prepareStatement(SQL);
  52. String aux;
  53. INT aux2;
  54. Scanner IN = NEW Scanner(System.IN);// Preenche os valores, respeitando a ordem e o tipo dos dados
  55. aux = IN.nextLine();
  56. stmt.setString(1, aux); // CHAR(8)
  57. aux2 =  INTEGER.parseInt(IN.nextLine());
  58. stmt.setInt(2, aux2); // NUMBER(4)
  59. aux2 = INTEGER.parseInt(IN.nextLine());
  60. stmt.setInt(3, aux2); // NUMBER(6)
  61. aux = IN.nextLine();
  62. stmt.setString(4, aux); // VARCHAR(50)
  63. aux = IN.nextLine();
  64. stmt.setString(5, aux); // VARCHAR(50)
  65. stmt.EXECUTE();
  66.      
  67.        
  68.     }
  69.    
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement