🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: uch.ing.dcc
Article #40

Pregunta JDBC (PreparedStatement)

#40
From: Manuel Villarroel
Date: Mon, 30 Jan 2006 18:02
148 lines
6762 bytes
This is a multi-part message in MIME format.

------=_NextPart_000_0017_01C625AE.2B5907B0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Estimados:

Alguien sabe si en una consulta se puede parametrizar el nombre de la =
tabla o de los campos
usando java.sql.PreparedStatement?

Ejemplo pseudo java :=20

/* 'con' es una conexion activa*/

String query =3D "Select * from ? where id =3D ?";
PreparedStatement ps =3D con.prepareStatement(query);

...
if(algo){
    ps.setString(1, "tabla1");
    ps.setInt(2, 1);
}
else{
    ps.setString(1,"tabla2");
    ps.setInt(2,1);
}
ps.executeQuery();
...

Mirando las consultas que se originan (utilizando el metodo toString() =
de PreparedStatement), en=20
la consulta generada aparecen quotes encerrando el nombre de la tabla, =
produciendo consultas
inv=E1lidas. He probado usando varios de los setters de =
PreparedStatement, pero siempre coloca los quotes.

Select * from 'tabla1' where id =3D 1
Select * from 'tabla2' where id =3D 1

ERROR:  syntax error at or near "'table1'" at character 16
LINE 1: Select * from 'tabla1' where id =3D 1

Dado que el codigo entre los puntos suspensivos se ejecuta dentro de un =
ciclo,lo que quiero evitar=20
es reemplazar, concatenar strings para generar una consulta, que mas =
encima se compilara cada vez.
(Aunque no estoy muy seguro de c=F3mo funciona exactamente =
PreparedStatement en ese sentido)
Por si acaso, estoy usando PostgreSQL 8.0


Alguna sugerencia?

--
Saludos,=20
Manuel Villarroel R.
------=_NextPart_000_0017_01C625AE.2B5907B0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2802" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Estimados:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Alguien sabe si en una consulta se =
puede=20
parametrizar el nombre de la tabla o de los campos</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>usando =
java.sql.PreparedStatement?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Ejemplo pseudo java : </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>/* 'con' es una conexion =
activa*/</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>String query =3D "Select * from ? where =
id =3D=20
?";</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>PreparedStatement ps =3D=20
con.prepareStatement(query);</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>if(algo){</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; ps.setString(1,=20
"tabla1");</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; ps.setInt(2, =
1);</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>}</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>else{</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;=20
ps.setString(1,"tabla2");</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; =
ps.setInt(2,1);</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>}</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>ps.executeQuery();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Mirando las consultas que se originan =
(utilizando=20
el metodo toString() de PreparedStatement), en </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>la consulta generada aparecen quotes =
encerrando el=20
nombre de la tabla, produciendo consultas</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>inv=E1lidas. He probado usando varios =
de los setters=20
de PreparedStatement, pero siempre coloca los quotes.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2>Select * from 'tabla1' where id =3D =
1</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Select * from 'tabla2' where id =3D =
1</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>ERROR:&nbsp; syntax error at or near =
"'table1'" at=20
character 16<BR>LINE 1: <FONT face=3DArial size=3D2>Select * from =
'tabla1' where id=20
=3D 1</FONT></FONT></DIV>
<DIV>&nbsp;</DIV></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Dado que el codigo entre los puntos =
suspensivos se=20
ejecuta dentro de un ciclo,lo que quiero evitar </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>es reemplazar, concatenar strings para =
generar una=20
consulta, que mas encima se compilara cada vez.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>(Aunque no estoy muy seguro de c=F3mo =
funciona=20
exactamente PreparedStatement en ese sentido)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Por si acaso, estoy usando PostgreSQL=20
8.0</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Alguna sugerencia?</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>--<BR>Saludos, <BR>Manuel Villarroel=20
R.</FONT></DIV></BODY></HTML>

------=_NextPart_000_0017_01C625AE.2B5907B0--
Technical Details
Message-ID: <drlkg4$csr$1@helcaraxe.dcc.uchile.cl>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!news.cec.uchile.cl!helcaraxe.dcc.uchile.cl!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: faaa5,c949046c6148f2b0,start
X-Google-Thread: 1593cb,c949046c6148f2b0,start
X-Google-Attributes: gidfaaa5,gid1593cb,public
X-Google-Language: SPANISH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!news.cec.uchile.cl!helcaraxe.dcc.uchile.cl!not-for-mail
From: "Manuel Villarroel" <someone@somewhere.com>
Newsgroups: uch.ing.dcc,chile.comp.programacion
Subject: Pregunta JDBC (PreparedStatement)
Date: Mon, 30 Jan 2006 15:02:20 -0300
Organization: DCC - Universidad de Chile
Lines: 149
Message-ID: <drlkg4$csr$1@helcaraxe.dcc.uchile.cl>
NNTP-Posting-Host: anakena.dcc.uchile.cl
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="----=_NextPart_000_0017_01C625AE.2B5907B0"
X-Trace: helcaraxe.dcc.uchile.cl 1138644292 13211 192.80.24.6 (30 Jan 2006 18:04:52 GMT)
X-Complaints-To: newsmaster@dcc.uchile.cl
NNTP-Posting-Date: Mon, 30 Jan 2006 18:04:52 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180