import java.sql.*;
/**
* This is an effort to get a computed value from a Java function
* (or data object) included properly in the VALUES entries.
* So, how can I declare an SQL variable and set its value to some Java value?
* Results viewed in pgAdmin3 with query 'select * from public.hello'.
*
* Jar in classpath is postgresql-9.2-1002.jdbc4.jar
*
* @version 0.1 Mon Jan 28 EST 2013
* @author Bob Futrelle
*/
public class JDBCVariableTest {
Connection db;
Statement st;
Boolean boo;
public static void main(String[] args) throws SQLException {
JDBCVariableTest testIt = new JDBCVariableTest();
testIt.helloVariables();
}
public int f1() { return 3;}
public void helloVariables() throws SQLException {
int intVar = f1(); // OK in Java, but SQL/JDBC?
try {
db = DriverManager.getConnection("jdbc:postgresql:Articles", "robertfutrelle", "<my pw>");
st = db.createStatement();
boo = st.execute("CREATE TABLE IF NOT EXISTS hello ( Name VARCHAR NOT NULL PRIMARY KEY, value int)");
// Declare .... ??
// INSTEAD OF THE LITERAL 4 VALUE (which works)
// how do I declare a variable and assign the f1() return value to it
// and then include it so the value 3 appears in the inserted record?
//st.execute("insert into hello values('aKey',4)");
st.execute("insert into hello values('bKey',4)");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}