Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Concat java variable to a MySql select statement and exeucte

843840Sep 9 2008 — edited Sep 9 2008
Hi,

I am trying to append a variable to a MySql select statement.

Overview: I need to retrieve data from a MySql database with a java variable as a reference and select the data in the database based on that variable.

CODE THAT I CURRENTLY HAVE:
// Declare variables
Connection conn = null;
Statement st = null;
Resultset rs2 = null;
String st2 = null;
String keyid = null;

// Connect to database
try {

      Class.forName("org.gjt.mm.mysql.Driver").newInstance();
      conn = DriverManager.getConnection("jdbc:mysql://" + mysql_host + ":3306/" + mysql_database, mysql_login, mysql_password);
      st = conn.createStatement();
      // Select data in Database with hanging equal sign
      st2 = ("SELECT * FROM table WHERE keyid= ");
      // Append keyid to hanging equal sign of select statement
      rs2 = st.executeQuery(st2 + keyid);

}
This is not working when I try to display the data.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 7 2008
Added on Sep 9 2008
3 comments
639 views