import java.sql.*; public class Test { public static void main( String[] args ) { try { String jdbcUrl = "jdbc:mysql://localhost/test"; String username = "root"; String password = "root"; long id = 1; String query = "SELECT a.test_id " + "FROM test_a as a, " + " test_b as b " + "WHERE a.test_id = b.test_id " + "AND a.test_id = ?"; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection( jdbcUrl, username, password ); PreparedStatement ps = conn.prepareStatement ( query ); ps.clearParameters(); ps.setLong( 1, id ); ResultSet rs = ps.executeQuery(); while ( rs.next() ) { System.out.println("val : " + rs.getLong(1) ); } rs.close(); rs = null; } catch (Exception e) { System.err.println(e); } } }