// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SqlInjectB {
    private static void test(Connection con, String f2) throws SQLException {
        try(PreparedStatement pstmt = con.prepareStatement("SELECT COUNT(*) FROM t1 WHERE f2 = ?")) {
            pstmt.setString(1, f2);
            try(ResultSet rs = pstmt.executeQuery()) {
                rs.next();
                System.out.println(rs.getInt(1));
            }
        }
    }
    public static void main(String[] args) throws Exception {
        try(Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Test", "root", "hemmeligt")) {
            test(con, "BB");
            test(con, "BB' OR 'X'='X");
        }
    }
}
