Miniclase en java para hacer consultas a una BDD SQL de Oracle

he aquí una clase en java que acabo de hacer para conectarse a una base de datos SQL de Oracle. Facilita la vida y ahorra complicaciones. Esperemos que le sirva a alguien. También me servirá a mi mismo para no perderla por que sospecho que me volverá a hacer falta. Requiere añadir el .jar del driver específico de la versión de sql de Oracle que sirva la base de datos.

package bd;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
 
public class Database {
 
	private String host;
	private String user;
	private String password;
	private String port;
	private String dir;
	private Statement stmt;
	private Connection con;
	public Database() {
 
	}
	public Database(String h, String u, String p, String po, String s) {
		port=po;
		host=h;
		password=p;
		dir=s;
		user=u;
	}
 
	public boolean conectar() {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con=DriverManager.getConnection("jdbc:oracle:thin:@"+host+":"+port+":"+dir,user,password);
			stmt=con.createStatement();
			return true;
		}
		catch(Exception e){
			return false;
			}
	}
	public String ejecutarConsulta(String consulta) { // Cambia este método para hacer con la consulta lo que gustes
		try {
 
			ResultSet rs=stmt.executeQuery(consulta);
			String devolver="";
			while(rs.next()) {
				for(int i=1; i<=rs.getFetchSize(); i++) {
					devolver+=rs.getString(i)+" ";
				}
				devolver+="\n";
			}
			return devolver;
		}
		catch (Exception SQLException) {
			System.out.println("Saltó la escepción\n"+SQLException);
		}
		return "error";
	}
	public boolean desconectar() {
		try {
			con.close();
			return true;
		}
		catch (Exception SQLException) {
			return false;
		}
	}
}

La clase la podemos probar con algo como esto:

package bd;
 
public class Test {
 
	public Test() {
		// TODO Auto-generated constructor stub
	}
 
	public static void main(String[] args) {
		Database d=new Database("localhost", "hr", "hr", "1521", "xe");
		String c="String";
		System.out.println("Conectando con la base de datos:");
		if(d.conectar())
			c=d.ejecutarConsulta("select * from employees");
		else
			System.out.println("No se pudo conectar. Revisa los datos introducidos.");
		if(d.desconectar())
			System.out.println("Desconectado tras jecutar la consulta.");
		else
			System.out.println("Por alguna razón no se ha podido desconectar.");
		System.out.println(c);
	}
 
}

Lo dicho, espero que alguien le saque utilidad. Puede mejorarse, seguro.

3 comments

11
Mayo

Imagen de vfmBOFH

Atentos los escritores:

Si publicáis *cualquier cosa* que no sea una entrada de blog, acordaos de marcar que se promueva a la portada.

La opción está en "Opciones de publicación".

Acabaré unificando todo en una categoría y hastaluegolucas.

Atentamente, La voz ésa del interior de tu cabeza que oyes cuando lees algo.

6
Jun

Hola!

Si vas a concatenar un String tantas veces, mira la clase String builder. Será mucho más óptimo.

Saludos!

18
Ene

moncler jackets
canada goose
coach factory outlet
christian louboutin outlet
ugg boots
canada goose jackets uk
yeezy 350 boost
uggs outlet
ray ban sunglasses discount
coach factorty outlet
michael kors outlet
michael kors outlet
pandora charms
michael kors outlet online
canada goose outlet
moncler jackets
coach factory outlet online
canada goose sale
kate spade
polo outlet
coach outlet
hermens bags
pandora jewelry
canada goose outlet
ugg boots
moncler outlet
nike zoom
moncler jackets outlet
the north face
longchamp outlet
coach factory outlet
timberland outlet
coach factory outlet
ralph lauren outlet
michael kors outlet
cheap uggs
adidas yeezy
north face jackets
canada goose outlet
coach outlet online
louis vuitton outlet
cheap ray ban sunglasses
ralph lauren outlet
mulberry uk
cheap ugg boots
nike shoes
hermes handbags
ugg outlet
oakley sunglasses
ugg shoes
ugg outlet store
coach outlet
fitflops sale clearance
jordan retro 11
adidas nmd
moncler outlet
burberry
cheap nfl jerseys
pandora jewelry
pandora jewelry
nike outlet
air max 97
ugg boots
michael kors canada
canada goose jackets
coach factory outlet
christian louboutin outlet
birkenstocks
ugg boots
ralph lauren uk
coach handbags
ugg outlet
nike outlet
north face outlet online
michael kors outlet
coach outlet online
oakley sunglasses
michael kors outlet
burberry outlet store
ugg shoes
coach factorty outlet
moncler outlet
coach canada
coach factory outlet online
cheap jordans for sale
pandora outlet
air max 2018
ralph lauren outlet
ugg boots
nike shoes
ugg ustralia
michael kors outlet store
air jordan shoes
cheap ray bans
nike outlet store
michael kors outlet
ralph lauren sale clearance
michael kors handbags
harden vol 1
uggs outlet
michael kors outlet clearance
nike outlet online
timberland boots
ugg boots
fitflops
coach outlet
kate spade
adidas yeezy boost
pandora charms uk
coach factory outlet
ralph lauren outlet
nike outlet store
yeezy boost
uggs clearance
supreme clothing
michael kors outlet
cheap mlb jerseys china
coach canada
cheap mlb jerseys
nike store
ugg boots canada
pandora charms sale clearance
canada goose
pandora jewelry outlet
nike air max
adidas superstar
cheap uggs
uggs
yeezy boost
ugg outlet
nike shoes
ultra boost
adidas nmd
coach outlet
supreme clothing
moncler outlet
north face outlet
ugg boots
coach outlet
michael kors outlet clearance
canada goose jackets
fitflops
mbt shoes
oakley sunglasses
jordan retro
oakley sunglasses
coach outlet
canada goose
ray ban sunglasses
moncler jackets
pandora
cheap jordans
ferragamo shoes
adidas yeezy
moncler uk
adidas outlet
moncler outlet
north face jackets
adidas nmd
ugg outlet
canadian goose
nmd shoes
coach outlet store
canada goose outlet
michael kors outlet
ralph lauren outlet
adidas nmd
cat boots
air jordan shoes
canada goose
red bottom shoes
canada goose outlet
ralph lauren
louis vuitton
michael kors outlet clearance
polo ralph lauren outlet online
burberry outlet
polo ralph lauren outlet
mbt
air jordans
ugg outlet
canada goose clothing
ralph lauren outlet
coach outlet
yeezy boost
ray ban sunglasses
pandora charms
canada goose outlet
uggs canada
canada goose jackets
kate spade outlet store
cheap nfl jerseys
ralph lauren outlet
canada goose outlet
pandora charms sale clearance
michael kors handbags
hermes handbags
canada goose coats
canada goose uk
canada goose
north face jackets
pandora charms
longchamp uk
cheap jordans
uggs
hermes bags
coach factory outlet
canada goose uk
cheap jordans
ralph lauren outlet
michael kors outlet
salvatore ferragamo shoes
oakley sunglasses
uggs
kate spade outlet store
supreme clothing
ugg outlet
coach outlet
doudoune moncler
ralph lauren
longchamp outlet
columbia sportswear
coach outlet
pandora jewelry
canada goose uk
coach factory outlet
burberry outlet
cheap uggs
christian louboutin
canada goose outlet
ralph lauren outlet
ugg boots
nike shoes for men
ugg outlet
coach outlet store online clearances
pandora charms
canada goose outlet
ugg boots on sale
michael kors outlet
ugg boots for women
ralph lauren outlet
moncler jackets
nfl jerseys wholesale
canada goose outlet
coach outlet
mbt shoes
kate spade outlet
coach factory outlet
oakley sunglasses
cheap jordans
ugg boots
jordan shoes
pandora outlet
pandora charms sale clearance
coach factory outlet
timberland boots
pandora store
canada goose uk
coach outlet
canada goose sale
fred perry polo
air max 97
coach outlet store
polo ralph lauren
michael kors outlet
coach outlet
burberry outlet
yeezy boost
discount oakley sunglasses
pandora charms
canada goose outlet
adidas shoes
uggs canada
michael kors outlet
adidas outlet
coach outlet store
ugg outlet
canada goose sale
ugg outlet
moncler jackets
oakley sunglasses sale
coach factory outlet
pandora charms sale
kate spade handbags
louis vuitton outlet store
canada goose jackets
valentino shoes outlet
adidas yeezy boost
christian louboutin outlet
polo ralph lauren outlet online
polo ralph lauren outlet
burberry sale
kate spade outlet
canada goose outlet
pandora charms sale
coach outlet store
michael kors outlet clearance
adidas shoes
nmd adidas
pandora outlet
coach outlet
adidas yeezy
coach outlet store
mulberry handbags
michael kors
kate spade bags
cheap jordans
moncler coats
hermes birkin
jordans
kate spade outlet
michael kors outlet
coach outlet
ralph lauren uk
red bottoms shoes
adidas outlet
polo ralph lauren outlet
nmd adidas
fitflops sale clearance
polo ralph lauren outlet online
michael kors outlet clearance
ralph lauren uk
mulberry outlet
pandora charms
ugg outlet
louis vuitton outlet online
louboutin shoes
cheap jordans free shipping
tory burch outlet
ralph lauren sale clearance
adidas campus
coach factory outlet
canada goose uk
canada goose jackets
longchamp outlet online
supreme new york
fitflops sale
longchamp handbags
red bottoms
canada goose sale
ralph lauren outlet
coach outlet
coach factory outlet
north face jackets
philipp plein outlet
gucci outlet
nfl jerseys wholesale
canada goose jackets
coach bags
coach outlet store online
air jordans
polo outlet
canada goose outlet
ugg outlet
canada goose
ugg boots
michael kors handbags
canada goose outlet
ugg outlet
polo ralph lauren outlet online
yeezy boost 350
ugg australia
coach outlet
valentino shoes
tory burch outlet store
pandora charms
ralph lauren outlet
ferragamo outlet
canada goose
nike air max
moncler
cheap oakley sunglasses
adidas shoes
ugg outlet
longchamp handbags
jordan shoes
polo ralph lauren outlet online
ralph lauren outlet
coach outlet
ugg boots on sale
fred perry
michael kors outlet
cheap ray ban sunglasses
ugg outlet online
canada goose outlet
moncler outlet
adidas yeezy
adidas shoes
burberry outlet canada
ray ban sunglasses
ugg boots
moncler
adidas yeezy
canada goose jackets
nike outlet
michael kors uk
mulberry bags
ugg outlet
ugg boots
michael kors canada
north face outlet
louboutin shoes
hermes handbags
louis vuitton outlet
cheap nfl jerseys
ray ban sunglasses
christian louboutin
cheap oakley sunglasses
coach outlet
ugg outlet
canada goose outlet
jordan shoes
adidas superstar
pandora charms
michael kors outlet
michael kors outlet
canada goose jackets
birkenstock sandals
canada goose outlet
ugg outlet
gucci handbags
adidas yeezy boost
pandora jewelry
michael kors outlet online
coach outlet
polo ralph lauren outlet
michael kors factory outlet
fitflops
air max 2018
coach outlet
timberland boots outlet
uggs outlet
philipp plein shirt
ugg outlet
michael kors outlet online
polo ralph lauren outlet
adidas
ugg boots
louis vuitton outlet online
ralph lauren uk
ugg outlet
hermes outlet
north face outlet
air max 2017
kate spade handbag
ugg outlet
cheap ray ban sunglasses
coach outlet
salvatore ferragamo
ray ban sunglasses
coach factory outlet
adidas shoes
michael kors outlet store
yeezy boost
1.03linpingping