SunQuest
           Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old April 19th, 2001, 12:59 PM
wolfespawn wolfespawn is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 68 wolfespawn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via AIM to wolfespawn
Unhappy

I am strictly a java programmer that connects to an oracle database to do basic query jobs. I was reading in an Oracle tuning book that it would be much more efficient for me to use bind variables in my queries. All of my queries are written in JAVA and passed to the database through either a bean or JDBC connection and all queries are exactly the same with the exception of the variables in the where clause (i.e. where customer= 77447). I read that my query should read (i.e. where customer = :custNO) or something. How do I tell or pass what the custNO is from my java program to the oracle database? Does anyone have experience in this? I would need to somehow set the bind variable in JAVA and pass it the same way i pass the rest of the query, but i have no idea what to call it or how to pass it.... Sorry for the long question, I just want to make sure my problem is clear.... thanks to whoever has thoughts on this.

Reply With Quote
  #2  
Old April 19th, 2001, 06:29 PM
jasonw jasonw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Location: Columbia, SC
Posts: 5 jasonw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
What you need is called a 'prepared statement'. This way you can have a generic SQL statement and pass a parameter to it. For example...

// Load driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// Get connection
Connection con = DriverManager.getConnection("jdbc:odbc:yourdatabase", "", "");

// Setup PreparedStatement
PreparedStatement ps = con.prepareStatement("SELECT firstname,lastname FROM customers WHERE customer = ?");

// Setup ResultSet
ResultSet rs;

// Now this is where we setup the parameter to be passed to the statement

ps.setInt(77447); // Of course this could be some other variable

// Now get the results
rs = ps.executeQuery();

This is basically it. The "?" in the statement is the variable you pass in. You can have several question marks in the statement, but you must set their value in the order they appear in the statement. Also the value does not have to be an Int but it can be String,Float,Blob,etc. I hope this helps.

Cheers

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Stupid JAVA/SQL question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

IBM developerWorks




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway