Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package connection;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class Connect {
- public static Connection conexao;
- private static String servidor = "jdbc:mysql://127.0.0.1:3128/";
- public static boolean conectar() {
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- conexao = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:3128:xe", "andre", "root");
- if (conexao == null) {
- return false;
- }
- } catch (ClassNotFoundException e) {
- System.out.println(e.getMessage());
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- return true;
- }
- }
- package hospede;
- import java.io.IOException;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import connection.Connect;
- //@WebServlet("/Hospede")
- public class Hospede extends HttpServlet {
- private static final long serialVersionUID = 1L;
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- testar();
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- }
- private static void testar() {
- boolean conectado=Connect.conectar();
- String query = "select * from hospede";
- Statement busca = null;
- try {
- if (Connect.conexao!=null) {
- busca = Connect.conexao.createStatement();
- ResultSet resultado = busca.executeQuery(query);
- System.out.println(resultado.getString("nome"));
- }
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- }
- }
- package connection;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class Connect {
- private static final int porta = 1521;
- private static final String host = "127.0.0.1";
- private static final String servico = "xe";
- private static final String usuario = "andre";
- private static final String senha = "root"
- static {
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e) {
- throw new ExceptionInInitializerError(e);
- }
- }
- public Connection static conectar() throws SQLException {
- return DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":" + porta + ":" + servico, usuario , senha);
- }
- }
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import connection.Connect;
- //@WebServlet("/Hospede")
- public class Hospede extends HttpServlet {
- private static final long serialVersionUID = 1L;
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- testar();
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- }
- private static void testar() {
- String query = "select * from hospede";
- try (
- Connection c = Connect.conectar();
- Statement busca = c.createStatement();
- ResultSet resultado = busca.executeQuery(query)
- ) {
- System.out.println(resultado.getString("nome"));
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment