Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Prepared Statement for select query

843854Jul 9 2004 — edited Nov 20 2014
Hi All,

Can we use PreparedStatement for select query ?

if yes,then how

i mean if i write
PreparedStatement pstmt=con.prepareStatement("select * from Login where empno=? and password=?");

pstmt.setInt(eno);
pstmt.setString(pas);

then how do i see the results of this query?

Pooja

Comments

843854
You do this after prepared the statement:

ResultSet set = stmt.executeQuery();

while(set.next()){
//accessing the fields
a = set.getString(1);
....
}
800283
ResultSet resultSet = pstmt.executeQuery();
you need to change your sets to:
pstmt.setInt(1, eno);
pstmt.setString(2, pas);
843854
Hi All,

Can we use PreparedStatement for select query ?

if yes,then how

i mean if i write
PreparedStatement pstmt=con.prepareStatement("select *
from Login where empno=? and password=?");

pstmt.setInt(eno);
pstmt.setString(pas);

then how do i see the results of this query?

Pooja
u can do like this
ResultSet rs=pstmt.executeQuery();
while( rs.next()){
eno=rs.getString("empno");
password=rs.getString("password");
}
rs.close();
pstmt.close();
843859
I'm sorry but preparedstatement doesn't work for select queries. I'm being trying to use it, exactly as you point it in this forum, but i don't get response. The resultSet that i get doesn't have any actual response to the query i sent. I only contains the names of the columns i selected, all of the in just one column of the resultSet, since this set doesn't have more that just one column. Here's my code:
import java.io.*;
import java.sql.*;
import oracle.jdbc.pool.*;
import oracle.sql.BLOB;
import oracle.jdbc.*;
import oracle.sql.*;

public class SQLTest {
	public static void main(String args[]) throws ClassNotFoundException, SQLException {
		
		OracleDataSource ods = new OracleDataSource();

		ods.setUser("billeteraelectronica");
		ods.setPassword("sucre");
		ods.setDriverType("thin");
		ods.setServerName("127.0.0.1");
		ods.setDatabaseName("XE");
		ods.setPortNumber(1521);
		ods.setNetworkProtocol("tcp");
		
		String accountNumber = "0000000001";
		int sizeQuery = -1;

		Connection conn = ods.getConnection();
		String statement = "select ? from TRANSACCIONES where ACCOUNTNUMBER = ? order by FECHA desc";
		PreparedStatement pst = conn.prepareStatement(statement);
		pst.setString(1, "FECHA, CONCEPTO, MONTO, SALDO");
		pst.setString(2, accountNumber);
		ResultSet rs = pst.executeQuery();
		
		while(rs.next())
		{
				System.out.print(rs.getString("FECHA"));
				System.out.print("\t" + rs.getString("CONCEPTO"));
				System.out.print("\t" + rs.getString("MONTO"));
				System.out.println("\t" + rs.getString("SALDO"));
		}
		pst.close();
		conn.close();
	}
}
I get an error:

Exception in thread "main" java.sql.SQLException: Invalid column name

and if i change the rs.getString to an int, instead of the column name, i only have one column, if i try to acces to columnn number 2, i get an error. The only column has this result:

FECHA, CONCEPTO, MONTO, SALDO
FECHA, CONCEPTO, MONTO, SALDO
FECHA, CONCEPTO, MONTO, SALDO
FECHA, CONCEPTO, MONTO, SALDO

Since the query resulted in 4 fields.
If i use a normal Statement, i do get the resultSet.

Is this a huge bug? or am i doing something wrong?
843859
I found out that the problem is in the setString() method of the preparedStatement.
Everytime a setString() is used, it puts the String between ' '. So if i want to use a method to set the name of the column that i want to query, what method should i use?
843859
PreparedStatement is to be used to set the values, not to set the table or column names.
1 - 6
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 6 2008
Added on Jul 9 2004
6 comments
109 views