Logo 
Search:

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: 839
  

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();
thisTest.test();
}
public void tledatabase(){}

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


public void connect(){
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");

conn = DriverManager.getConnection
("jdbc:oracle:thin:@0.0.0.0:1521:DATA", "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
rset.close();
stmt.close();

Share: 

 

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
arguments.
i.e.
....
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.




Tagged: