Guest User

Untitled

a guest
Feb 25th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. package connection;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class Connect {
  8.  
  9. public static Connection conexao;
  10. private static String servidor = "jdbc:mysql://127.0.0.1:3128/";
  11.  
  12. public static boolean conectar() {
  13. try {
  14.  
  15. Class.forName("oracle.jdbc.driver.OracleDriver");
  16.  
  17. conexao = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:3128:xe", "andre", "root");
  18. if (conexao == null) {
  19. return false;
  20. }
  21. } catch (ClassNotFoundException e) {
  22. System.out.println(e.getMessage());
  23. } catch (SQLException e) {
  24. System.out.println(e.getMessage());
  25. }
  26.  
  27. return true;
  28. }
  29.  
  30. }
  31.  
  32. package hospede;
  33.  
  34. import java.io.IOException;
  35. import java.sql.ResultSet;
  36. import java.sql.SQLException;
  37. import java.sql.Statement;
  38.  
  39. import javax.servlet.ServletException;
  40. import javax.servlet.http.HttpServlet;
  41. import javax.servlet.http.HttpServletRequest;
  42. import javax.servlet.http.HttpServletResponse;
  43.  
  44. import connection.Connect;
  45.  
  46. //@WebServlet("/Hospede")
  47. public class Hospede extends HttpServlet {
  48. private static final long serialVersionUID = 1L;
  49.  
  50. @Override
  51. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  52. testar();
  53. }
  54.  
  55. @Override
  56. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  57.  
  58. }
  59.  
  60. private static void testar() {
  61. boolean conectado=Connect.conectar();
  62. String query = "select * from hospede";
  63. Statement busca = null;
  64. try {
  65. if (Connect.conexao!=null) {
  66. busca = Connect.conexao.createStatement();
  67. ResultSet resultado = busca.executeQuery(query);
  68. System.out.println(resultado.getString("nome"));
  69. }
  70. } catch (SQLException e) {
  71. System.out.println(e.getMessage());
  72. }
  73. }
  74. }
  75.  
  76. package connection;
  77.  
  78. import java.sql.Connection;
  79. import java.sql.DriverManager;
  80. import java.sql.SQLException;
  81.  
  82. public class Connect {
  83.  
  84. private static final int porta = 1521;
  85. private static final String host = "127.0.0.1";
  86. private static final String servico = "xe";
  87. private static final String usuario = "andre";
  88. private static final String senha = "root"
  89.  
  90. static {
  91. try {
  92. Class.forName("oracle.jdbc.driver.OracleDriver");
  93. } catch (ClassNotFoundException e) {
  94. throw new ExceptionInInitializerError(e);
  95. }
  96. }
  97.  
  98. public Connection static conectar() throws SQLException {
  99. return DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":" + porta + ":" + servico, usuario , senha);
  100. }
  101. }
  102.  
  103. import javax.servlet.ServletException;
  104. import javax.servlet.http.HttpServlet;
  105. import javax.servlet.http.HttpServletRequest;
  106. import javax.servlet.http.HttpServletResponse;
  107.  
  108. import connection.Connect;
  109.  
  110. //@WebServlet("/Hospede")
  111. public class Hospede extends HttpServlet {
  112. private static final long serialVersionUID = 1L;
  113.  
  114. @Override
  115. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  116. testar();
  117. }
  118.  
  119. @Override
  120. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  121.  
  122. }
  123.  
  124. private static void testar() {
  125. String query = "select * from hospede";
  126. try (
  127. Connection c = Connect.conectar();
  128. Statement busca = c.createStatement();
  129. ResultSet resultado = busca.executeQuery(query)
  130. ) {
  131. System.out.println(resultado.getString("nome"));
  132. } catch (SQLException e) {
  133. e.printStackTrace();
  134. }
  135. }
  136. }
Add Comment
Please, Sign In to add comment