Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Using an Argument in Select Statement

  Asked By: Alma    Date: Apr 20    Category: Java    Views: 1859

I have been able to successfuly connect to my Oracle Database using
the JDBC but my select statement that I have used so far is static
and I would like to make it dynamic and pass a value to it. Can
anyone help me out and tell me how I can do this?

Here's a little portion of the code. I want to pass a value in place
of the log_field5 in the select statement.

public class tledatabase{
public static void main(String [] args){
tledatabase thisTest = new tledatabase();
public void tledatabase(){}

public void test(){
System.out.println("about to connect");
System.out.println("Connection complete");

public void connect(){
Connection conn = null;
try {

conn = DriverManager.getConnection
("jdbc:oracle:thin:@", "user", "password");
if (conn!=null){
System.out.println("connection made");
Statement stmt = null;
ResultSet rset = null;

stmt = conn.createStatement();

// Execute Statement to get product Information
from the Database
rset = stmt.executeQuery(" SELECT log_field1 " +
" FROM " + "transaction0 where log_field5
= '596612'");

while (rset.next())
System.out.println(rset.getString ("log_field1"));

// Close Result Set and Statement



2 Answers Found

Answer #1    Answered By: Al Allen     Answered On: Apr 20

Not really sure what you're exactly trying to do but
you can always just concatenate the string using your
resultSet = stmnt.executeQuery("Select * from " +
tableName + " where log_field1 = '" + value + "'");

Answer #2    Answered By: Viola Hanson     Answered On: Apr 20

This is how I will do it:
String myVar= "8888888";
rset = stmt.executeQuery(" SELECT
log_field1 " + " FROM " + "transaction0 where "+
"'log_field5='" +myVar+ ")";

Didn't find what you were looking for? Find more on Using an Argument in Select Statement Or get search suggestion and latest updates.