Guest User

Untitled

a guest
Oct 31st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1.  
  2. import java.sql.*;
  3.  
  4. public class gdi_Projeto {
  5.  
  6.  
  7.  
  8. public static void main(String[] args) throws SQLException, ClassNotFoundException {
  9.  
  10. // Primeiro Registrar e carregar o Drive
  11.  
  12. String driver = "oracle.jdbc.driver.OracleDriver";
  13. Class.forName(driver);
  14.  
  15. // cria uma conexão
  16.  
  17. String password = "pass"; // senha do Oracle
  18. String user = "login"; // login do Oracle
  19. String url = ""; // url depende do Driver
  20.  
  21. // cria uma conexão;
  22.  
  23. Connection con = DriverManager.getConnection(url, user, password);
  24.  
  25.  
  26. // repassar comandos SQL criamos uma "demonstração" (em ingles Statement)
  27.  
  28. Statement stmt = con.createStatement();
  29.  
  30. String sql = "ComandoSQL";
  31.  
  32. stmt.execute(sql); // executa comandos que retornam múltiplos resultados.
  33.  
  34. stmt.executeQuery(sql); // executa comandos que retornam alguma resposta.
  35.  
  36. stmt.executeUpdate(sql); // executa comandos de INSERT, UPDATE, DELETE.
  37.  
  38. //Os resultados retornados pelo statement colocamos no resultSet
  39.  
  40. ResultSet rs = stmt.executeQuery(sql);
  41.  
  42. // Quando os comandos de SQL são repetidos multiplas vezes usa-se o preparedStatement
  43.  
  44. String pStmt = "UPDATE USUARIO SET SENHA = ? WHERE LOGIN = ?";
  45.  
  46. PreparedStatement pstmt = con.prepareStatement(sql);
  47.  
  48. pstmt.setString(1, "Lucas");
  49.  
  50. pstmt.setString(2, "007007");
  51.  
  52.  
  53. rs.close(); //libera os recursos quando acaba a consulta aos dados.
  54.  
  55.  
  56.  
  57. Statement type = con.createStatement();
  58.  
  59. String cmd = "";
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. }
  71. }
Add Comment
Please, Sign In to add comment