Advertisement
Guest User

Untitled

a guest
Feb 15th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. package br.com.jdbc;
  2.  
  3. import java.sql.*;
  4.  
  5. public class ConectaBanco {
  6.  
  7. public static void main(String [] args) {
  8. try {
  9. Class.forName("oracle.jdbc.driver.OracleDriver");
  10. Connection con = DriverManager.getConnection("jdbc:oracle:thin:user/pass@host:1522/base");
  11.  
  12. Statement stmt = con.createStatement();
  13. String sql ="Select * from acesso where nrdocumento ='999999999999'";
  14. ResultSet res = stmt.executeQuery(sql);
  15.  
  16. while (res.next()); {
  17. String hash = res.getString(3);
  18. System.out.println(hash);
  19. }
  20.  
  21. con.close();
  22. } catch (Exception e ) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27.  
  28. finally {
  29. if(stmt != null) {
  30. stmt.close();
  31. }
  32. if(rs != null) {
  33. rs.close();
  34. }
  35. if(con != null) {
  36. con.close();
  37. }
  38. }
  39.  
  40. stmt = con.createStatement();
  41. sql = " SUA SEGUNDA QUERY COM HASH = '" + hash + "' ";
  42. rs = stmt.executeQuery(sql);
  43. while (rs.next()) {
  44. //leitura do resultado
  45. }
  46.  
  47. public static void main(String [] args) {
  48. Connection con = null;
  49. ResultSet rs = null;
  50. Statement stmt = null;
  51. try {
  52. String sql = "";
  53. String hash = "";
  54. Class.forName("oracle.jdbc.driver.OracleDriver");
  55. Connection con = DriverManager.getConnection("jdbc:oracle:thin:user/pass@host:1522/base");
  56.  
  57. stmt = con.createStatement();
  58. sql = " Select * from acesso where nrdocumento ='999999999999' ";
  59. rs = stmt.executeQuery(sql);
  60. while (rs.next()) {
  61. hash = rs.getString(3);
  62. }
  63. rs.close();
  64.  
  65. stmt = con.createStatement(); //não tenho certeza se precisa chamar novamente
  66. sql = " SUA SEGUNDA QUERY COM HASH = '" + hash + "' ";
  67. rs = stmt.executeQuery(sql);
  68. while (rs.next()) {
  69. //leitura do resultado
  70. }
  71. } catch (SQLException e ) {
  72. e.printStackTrace();
  73. } finally {
  74. if(stmt != null) {
  75. stmt.close();
  76. }
  77. if(rs != null) {
  78. rs.close();
  79. }
  80. if(con != null) {
  81. con.close();
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement