
April 19th, 2001, 06:29 PM
|
|
Registered User
|
|
Join Date: Jun 1999
Location: Columbia, SC
Posts: 5
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
|